public static GameObject Find (string name);

Descripción

Encuentra un GameObject por el name y lo retorna.

Esta función solo retorna GameObjects activos. Si no hay un GameObject con un name a encontrar, null se retorna. Si el name contiene un carácter '/' , recorre la jerarquía como un nombre de ruta.

Por razones de rendimiento, se recomienda no utilizar esta función cada frame. En su lugar, almacene en caché el resultado en una variable miembro al inicio, o utilice 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.

Por razones de rendimiento, se recomienda no utilizar esta función cada 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;

// 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); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961