GameObject.Find

public static GameObject Find(string name);

 

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.Find.

 

例:

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"); } }
posted @ 2020-08-12 10:42  流星曳尾  阅读(286)  评论(0编辑  收藏  举报