How to assign a GameObject or Component in the INSPECTOR in Unity

Introduction

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.


Initial conditions

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.

script to show ways to find gameobjects references in unity scene
Fig. 1: Script we are going to use to see the different methods to find references of objects from the scene in Unity.

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.

Fig. 2: Hierarchy of the scene we are going to use to find the references of scene objects in Unity.
Fig. 3: Inspector window from a GameObject, the script to find references is assigned to this GameObject.

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.

declaration of a gameobject data type in unity script with public visibility, this allows to assign the reference from the inspector
Fig. 4: Declaration of a GameObject field with public visibility.

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.

public field in the inspector at unity
Fig. 5: In the inspector we see that the public field appears.
manual reference assignment in unity
Fig. 6: The object to be found has been manually assigned to the GameObject field in the inspector.

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.

message indicating that the reference to an object in unity has been successfully found.
Fig. 7: When entering the game mode we see the console message with the name of the object.

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.

definition of a private serialized field in Unity script, this makes it appear in the inspector but other scripts can't access it
Fig. 8: The field is defined with private visibility but serialized, this way is still visible in the inspector.

With this the variable “objectToFind” will be visible in the inspector and you can assign the object manually.

Scroll to Top
Secured By miniOrange