Version: 2022.3
言語: 日本語
public static GameObject Find (string name);

説明

name で GameObject を検索し返します。

この関数は、アクティブなゲームオブジェクトのみを返します。name でゲームオブジェクトが見つからない場合は、null を返します。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); } }