Finds a GameObject by name
and returns it.
This function only returns active GameObjects. If no GameObject with name
can be found, null is returned. If name
contains a '/' character, it traverses the hierarchy like a path name.
For performance reasons, it is recommended to not use this function every frame. Instead, cache the result in a member variable at startup. or use GameObject.FindWithTag.
Note: If you wish to find a child GameObject, it is often easier to use Transform.FindChild.
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { public GameObject hand; void Example() { hand = GameObject.Find("Hand"); hand = GameObject.Find("/Hand"); hand = GameObject.Find("/Monster/Arm/Hand"); hand = GameObject.Find("Monster/Arm/Hand"); } }
GameObject.Find is useful for automatically connecting references to other objects at load time; for example, inside MonoBehaviour.Awake or MonoBehaviour.Start.
For performance reasons, it is recommended to not use this function every frame.
A common pattern is to assign a GameObject to a variable inside MonoBehaviour.Start, and use the variable in MonoBehaviour.Update.
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { private GameObject hand; void Start() { hand = GameObject.Find("/Monster/Arm/Hand"); } void Update() { hand.transform.Rotate(0, 100 * Time.deltaTime, 0); } }