前提・実現したいこと
Unityにて2D横スクロールアクションゲームを制作している初心者です。言語はC#です。
1度にInstantiateするアイテム(Prefab)の個数を制限する方法を模索中です。
発生している問題・エラーメッセージ
public intで現在Instantiateしている数とInstantiateできる最大数を宣言したのですが、この先が分かりません。
該当のソースコード
C#
1public class PresentGenerator : MonoBehaviour 2{ 3 public GameObject P1; 4 public GameObject P2; 5 public GameObject P3; 6 7 public int count; //インスタンスしている数 8 public int max; //インスタンスできる最大数 9 10 public float xMinPosition; //X座標の最小値 11 public float xMaxPosition; //X座標の最大値 12 public float yMinPosition; //Y座標の最小値 13 public float yMaxPosition; //Y座標の最大値 14 15 // Start is called before the first frame update 16 void Start() 17 {} 18 19 // Update is called once per frame 20 void Update() 21 {} 22 23 private Vector2 GetRandomPosition() 24 { 25 //それぞれの座標をランダムに生成する 26 float x = Random.Range(xMinPosition, xMaxPosition); 27 float y = Random.Range(yMinPosition, yMaxPosition); 28 29 //Vector2型のPositionを返す 30 return new Vector2(x, y); 31 } 32 void Presentor() 33 { 34 if (count < max) 35 { 36 GameObject Present = Instantiate(P1) as GameObject; //P1を生成する 37 Present.transform.position = GetRandomPosition(); //生成するアイテムの位置をランダムに設定する 38 39 GameObject Present2 = Instantiate(P2) as GameObject; //P2を生成する 40 Present2.transform.position = GetRandomPosition(); //生成するアイテムの位置をランダムに設定する 41 42 GameObject Present3 = Instantiate(P3) as GameObject; //P3を生成する 43 Present3.transform.position = GetRandomPosition(); //生成するアイテムの位置をランダムに設定する 44 } 45 } 46}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。