What type of object is "Level"

If I want to cache my FindObjectOfType() in the Start method, what kind of object is it?

And our object in the scene is named “Level” and the script on that object is also named “Level”, how does it know to find the script (which it is obviously(?) doing because we can call LoadGameOver directly) instead of the object?

1 Like

I am not an expert, but I will try to answer your question.

First: you can try to Serialize Field (where we usually serialize fields in Player.cs) and in Die() method call LoadGameOver() like this:

    [SerializeField] GameObject level;

    private void Die()
        // .........................

I tried this on my own, it works correctly. I don’t know is it better or worse than the approach shown to us in Lection 113.


The Level.cs is a script.

What is the class exactly? As I read on Wikipedia:

a class is an abstract data type, it is a template for creating objects that provide initial values for states: initializing variable fields and implementing the behaviour of functions or methods. Other abstract data types - metaclasses, interfaces, structures, enumerations - are characterized by some of their own, other features.

So, when you ask “what kind of object is it” - I answer:

  1. Class is a data type
  2. Level is a class what has it’s own type: Level

Any class is a data type, and any class have its own name, therefore any class name is the type of this class.

So, your type of object is a type of class. It is “Level”.

For the Unity and C# compiler, I guess, it looks like this:
Script Name = Level (like in a solution explorer we have Level.cs)
Name of the Game Object on the Scene = Level
Class Name = Level
Class Name = Type Name => Type Name = Level

therefore the systems know what it has to look for. Do you feel the difference now? Script Name, Name of The Game Object on The Scene and Class Name (Type Name) it is like the different layers/blocks of one system. Unity, like operating system, see the difference between folder, .exe, .pdf, knows easily understands it.

If you create on your PC’s Desktop a folder with name Call Of Duty (for the all CoD games what you have on your PC), and if you have at the Desktop at the same time Call Of Duty.pdf (gaming guide, for example), Call of Duty.exe - it would not be a problem for the system to feel the difference between different types of objects with the same names.

And, the third part of this explanation. I tried to make it on my own and all works! Here the code with my commentary:

public class Player : MonoBehaviour
    Level level; // it is the Level type of class which we will operate, because it has been declared in Level.cs
    // and level variable of this type of class which we will use to store the cached reference
    void Start()
        level = FindObjectOfType<Level>(); // search for our Level class! not just for Level.cs or Level Game Object/Prefab


    private void Die()
        level.LoadGameOver(); // here we call LoadGameOver method
// .......................

P.S. I have half-written the fourth notebook on 96 sheets of extremely detailed synopsis, including all references to theory, documentation, etc.
Conceptually, the topic is really not simple. It’s good that you asked this question, for me it’s like an opportunity to practice and test my knowledge. Sorry for my bad english.


The type of the object returned by FindObjectOfType is defined in the <>.

For example:

Test test = FindObjectOfType<Test>();
ABC abc = FindObjectOfType<ABC>();

We can access a method inside the Level instance/object because FindObjectOfType returns an object. And if it does not return an object, it returns null, and you would get a NullReferenceException error in that case if you tried to access the non-existent object.

Is this what you wanted to know?

See also:

3 posts were split to a new topic: Need help with NullReferenceException

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