How to find a GameObject in Unity

When developing games in Unity, one of the most common tasks is to find and interact with game objects. This is an important feature to learn which getting started with Unity.

In this article, we will explore different ways to find game objects in Unity using C#.

Do not use these functions in the Update method without a condition. Otherwise, it will search for the object every frame and affect the performance of the game. It’s wise to use them in the Awake or Start function.

The GameObject.Find Method:

The most straightforward method to find a game object is by using the GameObject.Find method. This method takes a string argument, which represents the path name of the object in the hierarchy view.

For example, if you have an object named “Player” nested under an empty object named “Characters,” you can find it using GameObject.Find("Characters/Player").

However, it’s important to note that using GameObject.Find can impact performance, especially if used frequently or in performance-sensitive areas like the Update function.

Here is a sample code to find a game object named “Player” with no parent and log its position.

using UnityEngine;

public class FindScript : MonoBehaviour
{
    GameObject player;
    // Start is called before the first frame update
    void Start()
    {
        player=GameObject.Find("Player");     
        Debug.Log(player.transform.position); 
        
    }

}

GameObject.FindWithTag:

To simplify the process of finding objects with a specific tag, Unity provides the GameObject.FindWithTag method. This method takes a string argument representing a tag and returns the first active game object with that tag.

Tags can be managed using the Tag Manager in the Unity editor, allowing you to assign meaningful tags to objects for easy identification and retrieval.

Here is a sample code to find an object with a tag “Ball” and log its position.

using UnityEngine;

public class FindScript : MonoBehaviour
{
    GameObject player;
    // Start is called before the first frame update
    void Start()
    {
        player=GameObject.FindWithTag("Ball");     
        Debug.Log(player.transform.position); 
        
    }

}

Using Transform and GetComponent

If you need to find a child game object of a specific parent object, you can use the transform.Find method. This method searches for a child object using its path name relative to the parent object. For instance, transform.Find("ChildObjectName") will return the transform of the child object with the specified name.

Additionally, if you need to find a component attached to a child object, you can combine GetComponentInChildren with transform.Find to retrieve the component from the child object.

Sample code to find a child with name “Box”.

using UnityEngine;

public class FindScript : MonoBehaviour
{
    GameObject player;
    // Start is called before the first frame update
    void Start()
    {
        player=transform.Find("Box").gameObject;     
        Debug.Log(player.transform.position); 
        
    }

}

Assigning game object using public variables

You can use a public variable to assign the object directly in the inspector. By assigning the game object to a variable, you can directly access its properties and components without the need for additional searches.

Here is an example script that allows you to assign the game object in the inspector window.

using UnityEngine;

public class FindScript : MonoBehaviour
{
    public GameObject player;
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(player.transform.position);         
    }

}

Here is how the inspector window looks like

Option to assign game object in the inspector.

It’s important to mention that finding game objects during runtime incurs a performance cost, especially when performed frequently or in computationally intensive situations.

Also, GameObject.Find doesn’t find inactive objects. But the above method of using a public variable allows you to assign even an inactive object.

In conclusion, Unity provides various methods to find game objects efficiently. Whether you choose to use the GameObject.Find method, leverage tags and the Tag Manager, or store object references in member variables, understanding the different ways to find game objects in Unity will greatly enhance your game development workflow. Remember to consider performance reasons when determining the most suitable method for your specific use case, and optimize your code accordingly.

If you have any other questions, leave them in the comment box below. For the next lesson, we will learn how to raycast in Unity.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.