Find GameObject by NAME 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.



All the IMPORTANT information is summarized in the following TUTORIAL FROM MY YOUTUBE CHANNEL




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.



How to find GameObject by Name in Unity

If we know the name of the GameObject in the scene we want to access we can use that information to find the reference in our Script. For that we use the “Find” method inside the GameObject class and we pass as parameter the name of the object we want to find, as shown in figure 4.

Instruction to find the reference of a scene GameObject by its name in Unity.
Fig. 4: Instruction to find the reference of a GameObject in the scene by its name.

When this instruction is executed, Unity will go through all the hierarchy looking for a GameObject with that name, if it finds it, it returns it and the reference remains in the variable “objectToFind”, but if it does not find it, this variable will have null value and it will give us the error of “NullReferenceException” if we try to access it.

To keep in mind, if we have more than one object that has exactly the same name, Unity will return the first one it finds in its registry, this can lead to ambiguities, i.e. we could get the reference of an object other than the one we are looking for.

To avoid embedding data directly inside the functions, we can define a String with the name of the object to be found and then execute the “Find” function passing that String as a parameter, thus avoiding Hard-Coding, as shown in Figure 5.

Fig. 5: A string is declared with the name of the object to be found, to avoid hard-coding.

Scroll to Top
Secured By miniOrange