Published on

How to get a game object from a script in Unity

  • Evelyn
    Full-time IndieDev.
    I'm Japanese, so please forgive me if my English is strange.

Please share this article if you like!

In this article, I will show you how to handle GameObjects from scripts.

  • Using GameObject.Find()
  • Use transform.Find()
  • Using GameObject.FindWithTag()
  • Using GameObject.FindGameObjectWithTag()
  • Using FindObjectsOfType()
  • Declare a prototype as a public GameObject and specify it from the Inspector
Table of Contents

Using GameObject.Find()

To get the Loader installed at this time, write the following to get it.

GameObject loader = GameObject.Find("Loader");

This method is intuitive and easy to understand because it directly finds the object to be found, but because of the heavy duty nature of this method, it is best to avoid moving it in Update() as much as possible. However, since this method is heavy, it is best to avoid moving it as much as possible in Update(). When using this method, it is often best to allocate it in a private variable during Start().

Use transform.Find()

Only the child elements of the target game object will be retrieved, but you can use the transform's Find method to get them with less load.

GameObject childGameObject = transform.Find("Game object name of the child element").gameObject;

There are the following differences between GameObject.Find() and transform.Find().

GameObject.Find() transform.Find()
search target Everything in the Hierarchy Child objects
Whether it is possible to get inactivity Cannot be obtained Can be obtained
Return value (type to be acquired) GameObject type Transform type

Using GameObject.FindWithTag()

We can get one game object from the scene that matches the tag name.

GameObject obj = GameObject.FindWithTag("Your any tag name");

Using GameObject.FindGameObjectWithTag()

Unity allows you to set a Tag for a GameObject, either directly in a script or via the Inspector's Tag. FindGameObjectWithTag() will search for the specified Tag and return the GameObject found.

Since Tags can be attached to multiple objects, there are times when you want to retrieve multiple objects at once. In this case, you can set the same Tag to the objects you want to retrieve and use the GameObject.FindGameObjectsWithTag() method to retrieve them all at once. Note that the return value of this method is an array, so you need to be careful.

GameObject[] tagsGameObject = GameObject.FindGameObjectsWithTag ("Your any tag name");

Using FindObjectsOfType()

FindObjectsOfType() can be used to retrieve specific components in a hierarchy. The following example retrieves all the Text present in the hierarchy.

Text[] uiTexts = FindObjectsOfType(typeof(Text)) as Text[];

Declare a prototype as a public GameObject and specify it from the Inspector

In the case of GameObjects, you can specify a Scene or Prefab to ensure that you get the GameObject right away. In the case of GameObjects, you can specify on the Scene or in the Prefab to ensure that the GameObject is already retrieved immediately after startup. The advantage of this feature is that since the GameObject is specified from the beginning, it does not need to be searched for by Find, making the process relatively light. However, you need to be careful because it will use memory.

In addition, the Find search cannot be used if the GameObject itself is not active. However, if you specify a GameObject in the Inspector, you can manipulate it regardless of whether it is active or not, so it is possible to set SetActive to false and display it as true.

[SerializeField] GameObject anyGameObject;

Summary of this article

This is how to get the non-hierarchical elements from the script. I would be happy to contribute to your game development.

Master how to get the elements in the hierarchy for efficient game development!

Please share this article if you like!