Находит объект с именем name
и возвращает его.
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.
вместо этого, при запуске кэшируйте (храните) результат функции в поле или используйте GameObject.FindWithTag.
Note: If you wish to find a child GameObject, it is often easier to use Transform.Find.
Note: If the game is running with multiple scenes then Find will search in all of them.
using UnityEngine; using System.Collections;
// This returns the GameObject named Hand in one of the Scenes.
public class ExampleClass : MonoBehaviour { public GameObject hand;
void Example() { // This returns the GameObject named Hand. hand = GameObject.Find("Hand");
// This returns the GameObject named Hand. // Hand must not have a parent in the Hierarchy view. hand = GameObject.Find("/Hand");
// This returns the GameObject named Hand, // which is a child of Arm > Monster. // Monster must not have a parent in the Hierarchy view. hand = GameObject.Find("/Monster/Arm/Hand");
// This returns the GameObject named Hand, // which is a child of Arm > Monster. 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.
Из соображений по производительности не рекомендуется использовать эту функцию каждый кадр,
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;
// Find the GameObject named Hand and rotate it every frame
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); } }