Question about referencing the ScoreBoard method

I don’t understand why the line ‘scoreBoard = FindObjectOfType<ScoreBoard();’ is necessary. Why isn’t just declaring ‘ScoreBoard scoreBoard’ enough to access the IncreaseScore() method?

Also, what would be other, cleaner alternatives to do the task that ‘FindObjectOfType<>()’ is doing right now? In other words, what are other commonly used ways to reference methods in another class?

Thank you very much.

Hi Knife,

Welcome to our community! :slight_smile:

When we declare a variable, they automatically initialised with a default value if we do not initialise the variable ourselves. In the case of scoreBoard, the default value is null (= no reference). We could have hundreds of ScoreBoard objects in our scene. Even if Unity knew that we wanted to assign an object to scoreBoard, how is it supposed to know which one to assign if we don’t tell it what to do?

If a component is already in the scene and there is no way to get the reference manually (or if it’s too much work), we usually look for it via FindObjectOfType.

Has your question been answered?


See also:

1 Like

Hi Nina,

Thank you very much for your answer. I think I understand better now, thanks to your explanation. Basically I could think of it like declaring an integer or string - when you declare an int, you need to give it a value for it to have any meaning. So you could say that “int a = 10;” is similar to “ScoreBoard scoreBoard = FindObjectOfType();”. In both cases, we are specifying a variable type, variable name and assigning a value to it.

However, if we wanted to have many ScoreBoard objects, then how would we assign them? FindObjectOfType wouldn’t work then, right?

Thanks.

There is a bit more to know about C#: value types and reference types. Value types are never null.

For example, when you declare int number;, the value of number will be 0 because the default value of int is 0. There are just a handful of value types, so I’d suggest to learn them by heart. Everything else is a reference type.

FindObjectOfType returns the “first” object of a type it finds. For this reason, we use it only if we have just one object of that type in our scene or if we do not look for a specific object.

Here is a “bad” example of how you could find different components:

ScoreBoard sb1 = GameObject.Find("Player").GetComponent<ScoreBoard>();
ScoreBoard sb2 = GameObject.Find("Score Board").GetComponent<ScoreBoard>();

The ScoreBoard component must be assigned to a game object named Player and to a game object named Score Board. Otherwise, you’ll get null returned.

That’s a fairly clumsy example because of the hard-coded strings and the slow Find methods.

If we know the component before we start the game, we could assign the reference ourselves in the inspector. We would use [SerializeField], which was covered in the Project Boost section. That’s how we typically “link” things in Unity.

Hey Nina,

Thanks a lot for the thorough answer.

I didn’t know that Find() was an option. Of course, using [SerializeField] is an option and now I have learned that using tags and FindWithTag() is another way to reference objects.

Thank you, I understand much better now :slight_smile:

1 Like

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

Privacy & Terms