質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

1回答

4417閲覧

Unityでにゃんこ大戦争のようなゲームを作っているのですがプレファボ化したキャラが一度Destroyするとほかのプレファボ化したそのキャラがDestroyしません

arken5104

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2021/04/11 03:11

前提・実現したいこと

Unityでにゃんこ大戦争のような、わんこが主体のゲームを作っているのですが
プレファボ化したキャラが一度Destroyするとほかのプレファボ化したそのキャラがDestroyしません。

キャラクターはwankoとnyankoの2つで両方ともうまく動作しません。

わんこのtagがplayerでにゃんこのタグがenemyです。

発生している問題・エラーメッセージ

1つ目 NullReferenceException: Object reference not set to an instance of an object Enemy+<Attack>d__12.MoveNext () (at Assets/Script/Enemy.cs:79) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <0847a0faf94444ccbaf1958021b27f54>:0) 2つ目 MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.GameObject.GetComponent[T] () (at <0847a0faf94444ccbaf1958021b27f54>:0) wanko+<Attack>d__10.MoveNext () (at Assets/Script/wanko.cs:72) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <0847a0faf94444ccbaf1958021b27f54>:0) 3つ目 NullReferenceException: Object reference not set to an instance of an object Enemy+<Attack>d__12.MoveNext () (at Assets/Script/Enemy.cs:79) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <0847a0faf94444ccbaf1958021b27f54>:0)

該当のソースコード

C#

1Enemy.cs 2 3using System.Collections; 4using System.Collections.Generic; 5using UnityEngine; 6 7public class Enemy : MonoBehaviour 8{ 9 public bool isAttack = false; 10 public bool isMove = true; 11 private Rigidbody2D rb; 12 public float EmoveSpeed = 1; 13 public float MaxHP = 2; 14 private GameObject player; 15 public float enemyHP; 16 17 // Start is called before the first frame update 18 void Start() 19 { 20 enemyHP = 2; 21 rb = GetComponent<Rigidbody2D>(); 22 player = GameObject.FindWithTag("Player"); 23 } 24 25 // Update is called once per frame 26 void Update() 27 { 28 29 if (isMove == true) 30 { 31 rb.velocity = new Vector2(EmoveSpeed, 0); 32 GetComponent<Animator>().SetBool("Attack", false); 33 rb.constraints = RigidbodyConstraints2D.None; 34 rb.constraints = RigidbodyConstraints2D.FreezeRotation; 35 } 36 else 37 { 38 if (isAttack == true) 39 { 40 isAttack = false; 41 StartCoroutine(Attack()); 42 } 43 } 44 45 if (enemyHP == 0) 46 { 47 Destroy(this.gameObject); 48 } 49 50 } 51 52 private void OnCollisionEnter2D(Collision2D collision) 53 { 54 if (collision.gameObject.tag == "Player") 55 { 56 isMove = false; 57 rb.constraints = RigidbodyConstraints2D.FreezeAll; 58 isAttack = true; 59 } 60 } 61 62 private void OnCollisionExit2D(Collision2D collision) 63 { 64 if (collision.gameObject.tag == "Player") 65 { 66 isMove = true; 67 isAttack = false; 68 } 69 } 70 71 private IEnumerator Attack() 72 { 73 GetComponent<Animator>().SetBool("Attack", true); 74 yield return new WaitForSeconds(1.2f); 75 player.GetComponent<wanko>().playerHP--; 76 isAttack = true; 77 } 78} 79 80 81wanko.cs 82 83using System.Collections; 84using System.Collections.Generic; 85using UnityEngine; 86 87public class wanko : MonoBehaviour 88{ 89 public bool isAttack = false; 90 public bool isMove = true; 91 private Rigidbody2D rb; 92 public float moveSpeed = 1; 93 private GameObject enemy; 94 public float playerHP = 2; 95 96 // Start is called before the first frame update 97 void Start() 98 { 99 rb = GetComponent<Rigidbody2D>(); 100 enemy = GameObject.FindWithTag("Enemy"); 101 playerHP = 2; 102 } 103 104 // Update is called once per frame 105 void Update() 106 { 107 if(isMove == true) 108 { 109 rb.velocity = new Vector2(-moveSpeed, 0); 110 GetComponent<Animator>().SetBool("Attack", false); 111 rb.constraints = RigidbodyConstraints2D.None; 112 rb.constraints = RigidbodyConstraints2D.FreezeRotation; 113 } 114 else 115 { 116 if(isAttack == true) 117 { 118 isAttack = false; 119 StartCoroutine(Attack()); 120 } 121 } 122 123 if (playerHP == 0) 124 { 125 Destroy(this.gameObject); 126 } 127 128 } 129 130 private void OnCollisionEnter2D(Collision2D collision) 131 { 132 if(collision.gameObject.tag == "Enemy") 133 { 134 isMove = false; 135 rb.constraints = RigidbodyConstraints2D.FreezeAll; 136 isAttack = true; 137 } 138 } 139 140 private void OnCollisionExit2D(Collision2D collision) 141 { 142 if (collision.gameObject.tag == "Enemy") 143 { 144 isMove = true; 145 isAttack = false; 146 } 147 } 148 149 private IEnumerator Attack() 150 { 151 Debug.Log(this.playerHP); 152 GetComponent<Animator>().SetBool("Attack", true); 153 yield return new WaitForSeconds(0.8f); 154 enemy.GetComponent<Enemy>().enemyHP--; 155 156 isAttack = true; 157 } 158}

補足情報(FW/ツールのバージョンなど)

バージョン
2019.4.17f1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

コードの全てを理解はできないが、エラーメッセージ2個目を読む限りnullチェックが良さそう?

投稿2021/04/11 09:07

Jyha1420B1G7DHP

総合スコア9

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問