1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Unity ゲームオブジェクトの取得方法を整理する

Last updated at Posted at 2023-08-24

Unityの肝であるゲームオブジェクトの取得方法について整理する

新しいものを見つけ次第ついきしていく

GameObject.Find

概要

  • 名前でオブジェクトを検索してリターンする関数

特徴

  • アクティブ名オブジェクトのみリターン
  • ゲームオブジェクトが見つからない場合はnullを返す
  • /が含まれている場合は、階層を探索する
  • 処理は重いみたい
public class ExampleClass : MonoBehaviour
{
    public GameObject hand;

    void Example()
    {
        // ハンドという名前のGameObjectを返す
        hand = GameObject.Find("Hand");

        // ハンドという名前のGameObjectを返す
        // ルートオブジェクトでないといけない
        hand = GameObject.Find("/Hand");

        // ハンドという名前のGameObjectを返す
        // Monster/Armの階層下にいなければいけない
        // Monsterはルートオブジェクトでないといけない
        hand = GameObject.Find("/Monster/Arm/Hand");

        // ハンドという名前のGameObjectを返す
        // Monster/Armの階層下でないといけない
        hand = GameObject.Find("Monster/Arm/Hand");
    }
}

リファレンス

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?