Unity Term Book
コアとアーキテクチャ

GameObject

GameObject は Unity シーンにおける最も基本的な存在単位です。自身にロジックを持たない空のコンテナで、振る舞いはすべてアタッチされた Component から生まれます。Hierarchy・Instantiate・FindWithTag・Destroy と参照キャッシュの基礎を学ぶ入門ガイドです。

想像してみてください...

GameObject は舞台に立つ台本のない役者のようです。単体では何もできず、Component(Rigidbody、Collider、Script…)が衣装・武器・台本となって初めて動きます。

概念の詳細

Unity の Entity-Component アーキテクチャでは、 GameObject が Entity——それ自体にロジックを持たない識別子です。振る舞いはすべてアタッチされた Component から来ます。

すべての GameObject には常に Transform があります——削除できないコンポーネントで、位置・回転・スケールを担当します。他の Component は Inspector の Add Component から追加できます。

GameObject はツリー状の Hierarchy に生きます。Parent を動かすとすべての Child も一緒に動く——多くのパーツで構成される複雑なキャラクターを一体として扱う仕組みです。

パフォーマンス:GameObject.Find() はシーン全体を走査します。必ず Awake()参照をキャッシュし、プライベートフィールドに保存してください。

構造ダイアグラム

GameObject: "Player"

⚙ Transform

Position · Rotation · Scale

必須

⚙ Rigidbody

Mass · Drag · Gravity

⚙ Capsule Collider

Center · Radius · Height

⚙ PlayerCtrl (Script)

moveSpeed · jumpForce

Custom

ハンズオン手順

1

新しい GameObject を作成

Hierarchy → 右クリック → Create Empty、または組み込み形状(Cube、Sphere…)を選びます。

2

名前を付けて Tag を設定

Inspector で名前を付け(例: Player)、Tag を割り当ててスクリプトから探しやすくします。

3

Component をアタッチ

Inspector → Add Component → Rigidbody、Collider、そして C# スクリプトを追加します。

4

コードから使う

Instantiate() で複製、FindWithTag() で検索、Destroy() で削除します。

インタラクティブシミュレーター

Hierarchy で GameObject をクリックして確認。作成や削除も試してみましょう。

Unity Editor — Scene: SampleScene

Hierarchy

← GameObject を選択

コード例

基本
using UnityEngine;

public class GameObjectBasics : MonoBehaviour
{
  public GameObject enemyPrefab;

  void Start()
  {
      // Spawn an Enemy at (2, 0, 0)
      GameObject newEnemy = Instantiate(enemyPrefab,
                              new Vector3(2f, 0f, 0f),
                              Quaternion.identity);
      newEnemy.name = "Enemy_001";

      // Find by Tag (faster than Find by name)
      GameObject player = GameObject.FindWithTag("Player");

      // Destroy the enemy after 5 seconds
      Destroy(newEnemy, 5f);
  }
}

コード例

上級
using UnityEngine;

public class GameObjectAdvanced : MonoBehaviour
{
  [SerializeField] private GameObject bulletPrefab;
  [SerializeField] private Transform firePoint;

  // Cache components — avoid GetComponent() in Update
  private Rigidbody _rb;
  private Animator _anim;

  void Awake()
  {
      _rb = GetComponent<Rigidbody>();
      _anim = GetComponent<Animator>();
  }

  public void SpawnBullet()
  {
      if (bulletPrefab == null) return;

      GameObject bullet = Instantiate(bulletPrefab,
                              firePoint.position, firePoint.rotation);

      // Keep Hierarchy tidy: parent the bullet under a container
      GameObject pool = GameObject.Find("BulletContainer");
      if (pool != null) bullet.transform.SetParent(pool.transform);

      // Disable without freeing memory
      gameObject.SetActive(false);
  }
}

📌 要点

  • GameObject = 空のコンテナ;振る舞いは Component から
  • すべての GO に Transform がある——削除不可
  • SetActive(false) は非表示にしてもメモリは解放しない
  • 名前より Tag で探す方が速い

⚠️ よくあるミス

  • ❌ Update() 内で GetComponent<>() を呼ぶ

    毎フレーム実行——非常に重い

    ✅ Awake() でプライベートフィールドにキャッシュする

  • ❌ Update() 内で GameObject.Find() を呼ぶ

    毎フレーム Hierarchy 全体を走査

    ✅ [SerializeField] で Editor から参照をドラッグする