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.
In the following video we see how to create a code to assign GameObjects and components in the inspector 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.
Declaring the field with public visibility
The fields and variables declared within a Script can have three types of visibility: “public”, “private” and “protected”. Let’s consider only the first two cases, private and public visibility, in the first case the field declared as private will not be accessible from external contexts to the Script itself in which it is defined, while if it is declared with public visibility you can directly access this script from other Scripts. In Unity you can also see the public fields in the inspector.
In Figure 4, line 8 we declare a GameObject field called “objectToFind” indicating public visibility by placing the word “public” in front of it.
Figure 5 is a screenshot of the Inspector window of the GameObject that has the Script we are editing assigned, as can be seen, the field “objectToFind” appears in the inspector (something that was not seen in figure 3 with the Script in figure 1), which means that now we can manually assign the GameObject we want to store in the “objectToFind” variable, i.e. we can manually assign the object reference.
This can be done in two ways, one is by taking the GameObject from the hierarchy and dragging it to the field, while the other way is by using the circle icon with the dot that can be seen to the right of the field (in figure 5 or 6), this will display a window in which we can choose the object we want to assign.
When entering the game mode we see the message in console that shows the name of the object that was assigned in the field, this occurs due to the execution of the instruction that is observed in figure 4 (line 13) and confirms that we have the reference of the GameObject, since we were able to use it to read its name.
Alternative: Declare the field as private by including [SerializeField]
If you want to keep the variable with private visibility so that other Scripts cannot directly access that field, there is an alternative to make the field appear in the inspector, it consists of adding “[SerializeField]” before the declaration, as shown in figure 8.
With this the variable “objectToFind” will be visible in the inspector and you can assign the object manually.