Question in regard to understanding the code (timer, FindObjectOfType<Timer>();)

Hello,

I have some basic questions in regard of the code and if my understanding is correct.
It would be great if somebody could tell me if I got it right.

So first we create a variable called timer of type Timer (So I suspect that the type refers to the Timer class we created). So the variable type is a class we created ourselves like the question variable of type QuestionSO?

Then we store a reference to the Timer script (and not the Timer game object) which is a component of the Timer game object in the timer variable with: timer = FindObjectOfType Timer

Why is that additional reference needed? I can’t really wrap my head around it. I mean we have defined a variable timer (of type timer class), isn’t that enough to call the methods stored in timer class? Like with the question variable of type QuestionSO?

Thanks

Hi pschroed,

You are right. C# is a so-called type-safe programming language. There are no type-less objects or variables. When you create a class, you implicitely create a type along with it.

We do not create a timer (= our human word) of type Timer but a class named Timer or an object of type Timer. If timer is a variable, we create a variable of type Timer. And the same applies to QuestionSO and everything else. In my opinion, the easiest way to grasp C# and programming is to regard these things as what they are: classes, objects, variables. Not things from the real world.

Unless a class is static, we must create objects from classes to be able to call methods on the object. We cannot call any methods on classes unless the methods are static.

Variables have got default values, and variables are not objects. When creating a variable of type Timer, the default value of that variable is null (= no object reference). We need to explicitely assign an object reference. In our case, we call FindObjectOfType<Timer>(); to get a reference. In other cases, we might assign an object in the Inspector in the Unity Editor.

Did this clear it up? :slight_smile:


See also:

1 Like

Thanks, that cleared it up.

I didn’t know that the default value of the timer variable is null and you therefore have to make a reference.

But there is one thing I am not sure about:

FindObjectOfType<Timer>();

Does this refer to the game object Timer or the component Timer.cs which is attached to the game object.
Because reading the text one would think we refer to the game object, but in the snow boarder game we used it to make a reference to the component surfaceEffector2D.

1 Like

Maybe one thing about C#: We have value types and reference types. The default value of the latter is always null. Learn the value types, everything else is a reference type.

Does this refer to the game object Timer or the component Timer.cs which is attached to the game object.

FindObjectOfType always refers to the component object. If you want to find a game object via its name, you could call GameObject.Find("Name");.

Because reading the text one would think we refer to the game object, but in the snow boarder game we used it to make a reference to the component surfaceEffector2D.

Maybe it helps to regard Unity and the C# side as two different things. A C# object is not something you can see anywhere because it’s just invisible data in the memory. Unity just visualises certain things but one Unity “thing” can be multiple C# objects.

Since C# objects do not have any names, we are not able to find any objects via their name. However, it might be that a class contains a variable named name, and that variable is very likely a string. For this reason, if you do something in your code which refers to something you typed in the Unity Editor, you usually use "". See GameObject.Find("Name");. The <> always wrap a type.

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

Privacy & Terms