unityにて、簡単なシューティングゲームを作っています
画面の右側にプレイヤーの残機を表示させようと思い、残機用の画像をプレハブ化し、以下のコードで実装を試みました(関数check_rem内)
for文で残機数の数だけループを回し、プレハブをインスタンス化していこうと考えたのですが、the variable Remaining of HeroScript has not been assignedと出てしまいます
主人公の発射する弾、hero_shotに関しては同様の実装で動いたのですが、残機については上手くいきません
hero_shot,Remainingのプレハブはheroscriptにアタッチしてあります
何か他に考えられる原因はあるでしょうか
よろしくお願いいたします
using
1using System.Collections.Generic; 2using UnityEngine; 3 4public class HeroScript : MonoBehaviour { 5 6 public GameObject hero_shot; 7 public GameObject Remaining; 8 public static int rem = 3; 9 bool flag = false; 10 // Use this for initialization 11 12 IEnumerator Start() 13 { 14 while(true){ 15 // 0.05秒待つ 16 yield return new WaitForSeconds(0.1f); 17 flag = true; 18 } 19 } 20 21 // Update is called once per frame 22 void Update () { 23 attack(); 24 move(); 25 26 check_rem(); 27 28 } 29 void check_rem() 30 { 31 for (int i = 0; i < rem; i++) 32 { 33 Vector2 rem_place = new Vector2(300+i*30, 130); 34 Instantiate(Remaining, rem_place, Quaternion.identity); 35 } 36 } 37 void attack() 38 { 39 Vector2 shot_place = new Vector2(this.transform.position.x, this.transform.position.y + 1); 40 if (Input.GetKey(KeyCode.Z)) 41 { 42 if (flag) 43 { 44 Instantiate(hero_shot, shot_place, Quaternion.identity); 45 flag = false; 46 } 47 } 48 } 49 void move() 50 { 51 Vector3 worldPos = this.transform.position; 52 Camera camera = Camera.main; 53 Vector3 viewportPos = camera.WorldToViewportPoint(worldPos); 54 if (Input.GetKey(KeyCode.UpArrow)) 55 { 56 if (viewportPos.y > 1.0f) return; 57 transform.position = new Vector2(this.transform.position.x, this.transform.position.y + 0.1f); 58 //transform.position += transform.forward * 0.01f; 59 } 60 if (Input.GetKey(KeyCode.DownArrow)) 61 { 62 if (viewportPos.y < 0f) return; 63 transform.position = new Vector2(this.transform.position.x, this.transform.position.y - 0.1f); 64 } 65 if (Input.GetKey(KeyCode.RightArrow)) 66 { 67 if (viewportPos.x > 1.0f) return; 68 transform.position = new Vector2(this.transform.position.x + 0.1f, this.transform.position.y); 69 } 70 if (Input.GetKey(KeyCode.LeftArrow)) 71 { 72 if (viewportPos.x < 0f) return; 73 transform.position = new Vector2(this.transform.position.x - 0.1f, this.transform.position.y); 74 } 75 } 76 77 public void gameover() 78 { 79 if (rem == 0) 80 { 81 this.gameObject.SetActive(false); 82 } 83 else 84 { 85 rem -= 1; 86 } 87 } 88} 89 90コード
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。