Using data from other scripts

Hello, all

I am sorry if this has already been answered, but I was wondering why do we have :
GameSession gameSession;
scoreText.text = gameSession.GetScore().ToString();
in ScoreDisplay script, but in Enemy script we use
FindObjectOfType().AddToScore(scoreValue);
Without assigning it to a variable as in both cases we are working with data that is returned from GameSession?

I am sorry if this seems like a stupid question and thanks in advance for the answer.

Hi Doncho,

At some point, we have to assign something to gameSession because the variable is null by default. We would get a NullReferenceException when we try to access something in the non-existing object. We can look for a GameSession object via the FindObjectOfType method.

Did this clear it up for you?


See also:

But why do we even create gameSession as a variable? I mean in Enemy script we do not create it as a variable. My best guess is that in ScoreDisplay we take data and in Enemy we provide data, is that the reason? Also thank you for the link with the code formatting.

How often do you access the GameSession object in this object and in the enemy object?

While doing the challenges in other videos I managed to get what I was missing, thank you for your time and effort.

You’re welcome. :slight_smile:


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms