GameObject.Find() / GetComponent<T>()
How to use GameObject.Find() to search for a GameObject in the scene by name, and GetComponent<T>() to retrieve an attached component.
Syntax
using UnityEngine; // Searches the scene for a GameObject by name (returns null if not found). GameObject obj = GameObject.Find(string name); // Searches for a GameObject by tag. GameObject obj = GameObject.FindWithTag(string tag); // Returns all GameObjects that match the given tag. GameObject[] objs = GameObject.FindGameObjectsWithTag(string tag); // Returns the component of type T attached to this GameObject. T comp = GetComponent<T>(); // Searches this GameObject and its children for a component of type T. T comp = GetComponentInChildren<T>(); // Searches this GameObject and its parents for a component of type T. T comp = GetComponentInParent<T>(); // Adds a component of type T to this GameObject and returns it. T comp = gameObject.AddComponent<T>();
Method List
| Method | Description |
|---|---|
| GameObject.Find(name) | Searches all active GameObjects in the scene by name. Inactive objects are not included. |
| GameObject.FindWithTag(tag) | Returns one GameObject with the specified tag. If multiple exist, an arbitrary one is returned. |
| GameObject.FindGameObjectsWithTag(tag) | Returns an array of all GameObjects with the specified tag. |
| GetComponent<T>() | Returns the component of type T attached to this GameObject, or null if none exists. |
| TryGetComponent<T>(out T component) | Attempts to retrieve a component. Returns true if found, false otherwise (Unity 2019.2 and later). |
| GetComponentInChildren<T>() | Searches this GameObject and its children (depth-first, including itself) for a component of type T. |
| gameObject.AddComponent<T>() | Adds a component of type T to this GameObject and returns it. |
Sample Code
using UnityEngine;
public class FindSample : MonoBehaviour
{
// Assigning via the Inspector is the recommended approach.
[SerializeField] private GameObject player;
[SerializeField] private Rigidbody2D myRigidbody;
void Start()
{
// GameObject.Find() — searches the scene by name (recommended to use in Start).
GameObject enemy = GameObject.Find("Enemy");
if (enemy != null)
{
Debug.Log($"Enemy found: {enemy.transform.position}");
}
// FindWithTag() — searches by tag (faster than Find()).
GameObject cam = GameObject.FindWithTag("MainCamera");
if (cam != null)
{
Debug.Log($"Main camera: {cam.name}");
}
// GetComponent<>() — retrieves a component.
Rigidbody rb = GetComponent<Rigidbody>();
if (rb != null)
{
rb.AddForce(Vector3.up * 10f);
}
// TryGetComponent — skips the null check (recommended).
if (TryGetComponent<Animator>(out Animator anim))
{
anim.SetTrigger("Jump");
}
// GetComponentInChildren — searches child objects for the component.
AudioSource sfx = GetComponentInChildren<AudioSource>();
if (sfx != null)
{
sfx.Play();
}
// Retrieves multiple objects by tag.
GameObject[] coins = GameObject.FindGameObjectsWithTag("Coin");
Debug.Log($"Coin count: {coins.Length}");
}
}
Notes
GameObject.Find() traverses the entire scene, so calling it every frame inside Update() will significantly hurt performance. Call it once in Start() or Awake() and cache the result in a variable. Where possible, prefer assigning references directly in the Inspector.
Accessing a component returned by GetComponent<T>() when it is null will throw a NullReferenceException. Always perform a null check, or use TryGetComponent<T>() instead. For debug output, see Debug.Log(). For Unity math functions, see Mathf.
If you find any errors or copyright issues, please contact us.