This article belongs to the series about finding GameObjects and Components references from the scene in Unity, in this series we will see different techniques to find from a Script any object that is in the hierarchy in a certain scene in Unity, this is something very important to understand and know how to do it because if we have the reference of an object or component, we can access it and read any public data or execute any public function we need.
Here you have the playlist about finding the references in Unity, in that list we will be adding videos with more and more specific techniques. The following video talks about the technique shown in this article:
In the following video we see what are “gameObject” and “transform” in Unity
We start from a Script called “FindReferenceOfAnObject” in which we are going to find the reference of a certain GameObject that is in the scene in Unity, inside the Script we will use that reference to print its name in console with the instruction of the line 13 of the figure 1.
The hierarchy of the scene that we are going to use is composed by the GameObjects that are shown in figure 2, the object “Script-GameObject” is the one that has the Script in figure 1 assigned and it is the one that will be in charge of finding the references, in figure 3 you can see the inspector of this GameObject, where the Script is assigned.
The “GDT (Object to Find)” object seen in figure 2 is the object we want to find from the script, so if we succeed we should see the name of this object printed on the console.
What are “gameObject” and “transform” in Unity – MonoBehaviour members
Any Script that we create that inherits from the MonoBehaviour class has inside a series of variables and functions that are already defined in the MonoBehaviour class and in the higher hierarchy classes from which MonoBehaviour inherits. In general when we create a new Script in Unity this inheritance relationship is established in this way, if you look at line 5 of figure 1, at the end ends with “: MonoBehaviour”, this tells Unity that the class that is being defined extends its behavior from the class “MonoBehaviour”, for more information on the subject I leave this article that talks about what is a class in Programming.
We can consult the Unity documentation to obtain more information about the MonoBehaviour class, there we will find all the members that are defined in that class, that is to say the variables and the functions, and also the inherited members of superior classes, in figure 4 we see a capture of the inherited variables and among them the “gameObject” and “transform” variables.
In the descriptions it tells us that: the “gameObject” variable points to the GameObject to which “this component” is assigned and says that a component is ALWAYS assigned to a GameObject. With “this component” refers to the MonoBehaviour on which you are working, in our case the Scripts that we are editing. And the variable “transform” interpreting the description tells us that it points to the Transform component of the GameObject to which this component is assigned. An important detail is that a GameObject ALWAYS has assigned at least one Transform component.
So if the GameObject that we want to use inside our Script is precisely the same GameObject to which the Script is assigned, the reference of this GameObject is already defined inside an internal field of the MonoBehaviour class, the field called “gameObject” (first letter with lowercase), this name refers to the same object to which the Script is assigned.
In this case it would not be necessary to define the “objectToFind” field, however we are going to initialize it as we have been doing so far. In line 17 of figure 5 we assign “gameObject” to “objectToFind”.
When entering the game mode, in figure 6 we see in the inspector that the object in the “Object To Find” field is the same object that has the Script assigned to it, you can tell because the name of the object in the “Object To Find” field is the same as the name of the GameObject (in the upper part of figure 6).