unityのシューティングゲームのチュートリルを追っているのですが、
https://unity3d.com/jp/learn/tutorials/projects/2d-shooting-game/creating-enemies?playlist=46524のコードで
C#
1using UnityEngine; 2using System.Collections; 3 4public class Enemy : MonoBehaviour 5{ 6 // Spaceshipコンポーネント 7 Spaceship spaceship; 8 9 IEnumerator Start () 10 { 11 // Spaceshipコンポーネントを取得 12 spaceship = GetComponent<Spaceship> (); 13 14 // ローカル座標のY軸のマイナス方向に移動する 15 spaceship.Move (transform.up * -1); 16 17 while (true) { 18 19 // 子要素を全て取得する 20 for (int i = 0; i < transform.childCount; i++) { 21 22 Transform shotPosition = transform.GetChild(i); 23 24 // ShotPositionの位置/角度で弾を撃つ 25 spaceship.Shot (shotPosition); 26 } 27 28 // shotDelay秒待つ 29 yield return new WaitForSeconds (spaceship.shotDelay); 30 } 31 } 32}
spaceship = GetComponent<Spaceship> ();という処理をコルーチンの中で行なっているのですが、いちいちループでspaceshipという変数を初期化するのは無駄な処理だと思い、
IEnumerator Start (){}の外にspaceship = GetComponent<Spaceship> ();を出したのですが、spaceshipは現在のコンテキストに存在しませんとエラーが起きてしまいます。
この処理はコルーチンの中で行う必要があるのでしょうか?
unity、C#初心者なので的はずれなことを言っていたら申し訳ありません。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。