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

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 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.

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 a GameObject in the scene by its TAG in Unity

One of the elements that every GameObject has in Unity is a Tag, it is located in the inspector header, as we can see in figure 4. We can use this tag to find the reference of the GameObject of the scene in our Script.

By default the GameObjects will have assigned the “Untagged” Tag, but we can assign one of the predefined Tags or create new Tags, in figures 4 and 5 I show how to create a new Tag in Unity.

Fig. 4: Tab to create a new Tag in Unity.
Fig. 5: Enter a name for the tag and save the changes.

Once we have created the Tag we have to assign it to the object, we select again the GameObject in the inspector, deploy the Tag drop-down menu and choose it, as shown in figure 6.

Fig. 6: The created Tag is assigned to the object to be referenced in the Script.

Now we are able to find that GameObject using the Tag, for this we execute the “FindGameObjectWithTag” method from the GameObject class and we pass as parameter the name of the Tag that we want to search. In figure 7 we see this instruction in line 16, notice that the name of the Tag has been defined in a String in line 11 and then in line 16 we pass as parameter that variable.

Fig. 7: Instruction to find the reference of a GameObject in the scene through its Tag.

When this instruction is executed, Unity will check all the objects in the hierarchy until it finds a GameObject that has that tag assigned, at that point it returns the object and it is stored in the “objectToFind” field. If there is no object with that Tag, the “objectToFind” field will have a null value.

To keep in mind, if we have more than one GameObject that has the same Tag assigned, Unity will return us the first of them that it finds in its registry, in this case some ambiguities could happen, we could obtain the reference of an object different from the one we want.



Exit mobile version
Secured By miniOrange