How to FIND a GameObject by TAG through code in Unity - FindGameObjectWithTag function

Опубликовано: 09 Ноябрь 2024
на канале: GDT Solutions ES
6,742
86

In this video we see how to FIND A GAMEOBJECT BY THE TAG that has assigned in the INSPECTOR in Unity, we will do this within a Script, we will define a variable that can store this object, then execute an instruction that finds the object of the scene by its tag and store the object in the variable, this allows us to have the reference of the object and thus be able to read its variables and execute its public functions.

This method requires that we know the TAG assigned to the GameObject we want to find, spaces and capital letters included. In Unity there can be many GameObjects that HAVE THE SAME TAG ASSIGNED, so we must be careful when searching for a GameObject in the scene by TAG in Unity because the reference we get could be a different object than the one we are looking for.

This method is used to find GameObjects at runtime, usually in some initialization function like Awake or Start. It is not recommended to use this method in an update function such as Update or FixedUpdate since finding an object by TAG in Unity requires, in the worst case, analyzing all objects in the hierarchy until finding the object that has the indicated TAG assigned to it.

The instruction to find a GameObject by TAG in Unity is:

GameObject.FindGameObjectWithTag("Name of the tag");

In the above instruction the static function "FindGameObjectWithTag" of the GameObject class is executed and the name of the TAG to be searched is passed as parameter.

-----------------------------------------------------------
IMPORTANT!!! DO NOT CONFUSE WITH:
GameObject.FindGameObjectsWithTag("Name of the tag");
The above instruction is a different function that searches for ALL GAMEOBJECTS that have the tag assigned to them.
-----------------------------------------------------------

The TAG can be entered directly between quotes as in this case, but you can also pass the TAG into a string variable that has the value of the TAG. This function goes through the hierarchy looking for a GameObject that has the INDICATED TAG assigned, when it finds it, it returns that GameObject and we can assign it to a GameObject variable.
If on the other hand there is no GameObject in the scene that has the indicated TAG assigned, the function returns null. If it was not possible to find a GameObject that has the indicated TAG assigned and then we try to use that GameObject in the code, either to read some variable or to execute some function, we will have a NullReferenceException error.

INTRODUCTION VIDEO OF THE REFERENCE SERIES IN UNITY:
   • Video  

ARTICLE ON HOW TO FIND A GAMEOBJECT BY NAME IN UNITY:
https://gamedevtraum.com/en/game-and-...

____________________________________________________________
LINKS

Portfolio: https://gamedevtraum.com/en/portfolio...

LinkedIn:   / gamedevtraum  

Downloads: https://gamedevtraum.itch.io/

Contact:   / gamedevtraum  
____________________________________________________________