LoginSignup
2
1

More than 5 years have passed since last update.

1つのゲームオブジェクトから複数の同一コンポーネントを取得

Last updated at Posted at 2017-10-14

unityチュートリアルのサバイバルシューターの中身をいじっていて、1つのゲームオブジェクトにEnemyManager.csが3つアタッチされていて戸惑ったのでメモ。

スクリーンショット 2017-10-14 21.27.17.png

GameObject.GetComponentsで、コンポーネントの配列を取得する。

private EnemyManager[] scripts;
private const int INTERVAL_UPDATE = 100;
private int nextTargetScore = INTERVAL_UPDATE;

void Start() {
    scripts = GameObject.Find("EnemyManager").GetComponents<EnemyManager>();
}

void Update() {
    // 100ptごとにspown率を更新
    if(score >= nextTargetScore){
        nextTargetScore += INTERVAL_UPDATE;
        foreach (var script in scripts)
        {
            script.UpdateSpown();
        }
    }
}

参考
同一のComponentを別々に取得

2
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
2
1