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

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

新規登録して質問してみよう
ただいま回答率
85.50%
Unity

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

Q&A

解決済

1回答

418閲覧

unity2D MissingReferenceExceptionのエラーについて

yoshiteru21

総合スコア44

Unity

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

0グッド

0クリップ

投稿2019/11/19 08:08

編集2019/11/19 08:09

ステージ1からステージ2にシーン移動した時、アイテムスロットに設定されてあるアイテムをVボタンでドロップするという処理を作りました。
するとエラーが起き、ネットで調べてみるとnullを使う必要があるとだけ書いてあったのですがどこにそれを追加すれば良いのかわかりません。

public

1{ 2 GameObject equipimagecolor; 3 GameObject EquipImage2; 4 GameObject EquipImage3; 5 GameObject Itemchange; 6 GameObject UnityChan; 7 GameObject Enemy1, Enemy1_2; 8 //アイテムのプレハブ 9 GameObject Soard; 10 GameObject Gun; 11 GameObject Tue; 12 13 EquipImageColor equipimagecolorScript; 14 EnemyItem enemyitemScript; 15 Action actionScript; 16 EquipSetImage2 equipsetImage2script; 17 EquipSetImage3 equipsetImage3script; 18 ItemGet_EquipChange itemgetScript; 19 20 public bool SoardOn = false; //剣モードのアクションにするためのもの 21 public bool GunOn = false; //銃モード " 22 public bool TueOn = false; //杖モード " 23 public bool EquipImageSet1 = false; 24 public static bool Soardon = false; //データ保持用 25 public static bool Gunon = false; // " 26 public static bool Tueon = false; // " 27 public static bool Equipimageset1 = false; 28 Image image; 29 Sprite sprite; 30 31 32 void Start() 33 { 34 EquipImage2 = GameObject.Find("EquipImage2"); 35 EquipImage3 = GameObject.Find("EquipImage3"); 36 equipimagecolor = GameObject.Find("Equip1"); 37 Itemchange = GameObject.Find("ItemPackage"); 38 Enemy1 = GameObject.Find("Enemy1"); 39 Enemy1_2 = GameObject.Find("Enemy1_2"); 40 UnityChan = GameObject.Find("UnityChan"); 41 itemgetScript = Itemchange.GetComponent<ItemGet_EquipChange>(); 42 equipsetImage2script = EquipImage2.GetComponent<EquipSetImage2>(); 43 equipsetImage3script = EquipImage3.GetComponent<EquipSetImage3>(); 44 enemyitemScript = Enemy1.GetComponent<EnemyItem>(); 45 actionScript = UnityChan.GetComponent<Action>(); 46 equipimagecolorScript = equipimagecolor.GetComponent<EquipImageColor>(); 47 //アイテムのプレハブをよぶ 48 Soard = (GameObject)Resources.Load("Prefabs/Item_Soard"); 49 Gun = (GameObject)Resources.Load("Prefabs/Item_Gun"); 50 Tue = (GameObject)Resources.Load("Prefabs/Item_Tue"); 51 52 EquipImageSet1 = Equipimageset1; 53 } 54 55 void Update() 56 { 57 if (!EquipImageSet1) 58 { 59 if ((equipsetImage2script.EquipImageSet2 == false && equipsetImage3script.EquipImageSet3 == false) || (equipsetImage2script.EquipImageSet2 == true && equipsetImage3script.EquipImageSet3 == true) 60 || (equipsetImage2script.EquipImageSet2 == true && equipsetImage3script.EquipImageSet3 == false)) //装備2,3にアイテムがない場合、または2、3にある場合 61 { 62 if (itemgetScript.Soard == true) 63 { 64 itemgetScript.Soard = false; 65 EquipImageSet1 = true; 66 SoardOn = true; 67 sprite = Resources.Load<Sprite>("Soard"); 68 image = this.GetComponent<Image>(); 69 image.sprite = sprite; 70 } 71 else if (itemgetScript.Gun == true) 72 { 73 itemgetScript.Gun = false; 74 EquipImageSet1 = true; 75 GunOn = true; 76 sprite = Resources.Load<Sprite>("Gun"); 77 image = this.GetComponent<Image>(); 78 image.sprite = sprite; 79 } 80 else if (itemgetScript.Tue == true) 81 { 82 itemgetScript.Tue = false; 83 EquipImageSet1 = true; 84 TueOn = true; 85 sprite = Resources.Load<Sprite>("Tue"); 86 image = this.GetComponent<Image>(); 87 image.sprite = sprite; 88 } 89 } 90 } 91 92 //アイテム削除のさいに使用 93 if (equipimagecolorScript.weapon == 0) 94 { 95 if (EquipImageSet1) 96 { 97 if (MyInput.MyInputKey(KeyCode.V)) 98 { 99 //それぞれのアイテムをドロップ 100 if (SoardOn) 101 { 102 SoardOn = false; 103 Instantiate(Soard, UnityChan.transform.position + new Vector3(0f, 1f, 0f), UnityChan.transform.rotation); 104 } 105 else if (GunOn) 106 { 107 GunOn = false; 108 Instantiate(Gun, UnityChan.transform.position + new Vector3(0f, 1f, 0f), UnityChan.transform.rotation); 109 } 110 else if (TueOn) 111 { 112 TueOn = false; 113 Instantiate(Tue, UnityChan.transform.position + new Vector3(0f, 1f, 0f), UnityChan.transform.rotation); 114 } 115 EquipImageSet1 = false; 116 image = this.GetComponent<Image>(); 117 image.sprite = null; 118 } 119 } 120 } 121 122 //保持しているデータをOnの方にも記録させる 123 if (Soardon) 124 { 125 SoardOn = true; 126 } 127 else if (Gunon) 128 { 129 GunOn = true; 130 } 131 else if (Tueon) 132 { 133 TueOn = true; 134 } 135 } 136コード 137
エラーが起きた場所 Instantiate(Soard, UnityChan.transform.position + new Vector3(0f, 1f, 0f), UnityChan.transform.rotation); コード
エラー内容 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. EquipSetImage1.Update () (at Assets/Scripts/EquipSetImage1.cs:111) コード

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

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

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

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

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

guest

回答1

0

ベストアンサー

cs

1MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. 2Your script should either check if it is null or you should not destroy the object. 3EquipSetImage1.Update () (at Assets/Scripts/EquipSetImage1.cs:111)

これに書いてあるようにDestroyしたものに対して参照しようとしている所が怪しいです。
コードがないのでわかりませんが、参照先がDestroyされていないかもあわせてチェックしてください
もしくはif(*** !=null){ }などのようにnullチェックをしながらデバッグをしても良いかと思います

投稿2019/11/20 00:28

hogefugapiyo

総合スコア3302

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

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

yoshiteru21

2019/11/20 01:15

ありがとうございます。早速試してみようかと思います。
yoshiteru21

2019/11/20 01:24

ちなみに参照先は上のスクリプトにある Soard = (GameObject)Resources.Load("Prefabs/Item_Soard") prefabsフォルダにあるItem_Soardから参照しているのでdestroyされていないと思うのですが別の参照先に問題があるということなのでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問