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

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

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

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

Q&A

1回答

994閲覧

unity MissingReferenceExceptionのエラーについて

yoshiteru21

総合スコア44

Unity

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

0グッド

0クリップ

投稿2019/11/21 02:45

編集2019/11/25 01:23

アイテムスロットにあるアイテムを選択しVボタンでドロップ(アイテムオブジェクトを生成)するという処理を作成し、うまくいったのですが
シーン移動後に同じ操作をやると下記のエラーが発生します。
参照先がDestroyされているのが原因とのことですが、参照先はproject内にあるprefab化されたオブジェクトから取得しているため、シーン内にはオブジェクトがなく、destroyされているはずがないと思うのですがどうなのでしょうか?
project内にあるprefabもdestroyされてしますのでしょうか?
解決方法のご教授お願いします。

スロット1のスクリプト(Instantiateの部分がエラーが発生した場所) using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using InputKey; public class EquipSetImage1 : MonoBehaviour { GameObject equipimagecolor; GameObject EquipImage2; GameObject EquipImage3; GameObject Itemchange; GameObject UnityChan; GameObject Enemy1, Enemy1_2; //アイテムのプレハブ GameObject Soard; GameObject Gun; GameObject Tue; EquipImageColor equipimagecolorScript; EnemyItem enemyitemScript; Action actionScript; EquipSetImage2 equipsetImage2script; EquipSetImage3 equipsetImage3script; ItemGet_EquipChange itemgetScript; public bool SoardOn = false; //剣モードのアクションにするためのもの public bool GunOn = false; //銃モード " public bool TueOn = false; //杖モード " public bool EquipImageSet1 = false; public static bool Soardon = false; //データ保持用 public static bool Gunon = false; // " public static bool Tueon = false; // " public static bool Equipimageset1 = false; Image image; Sprite sprite; void Start() { EquipImage2 = GameObject.Find("EquipImage2"); EquipImage3 = GameObject.Find("EquipImage3"); equipimagecolor = GameObject.Find("Equip1"); Itemchange = GameObject.Find("ItemPackage"); Enemy1 = GameObject.Find("Enemy1"); Enemy1_2 = GameObject.Find("Enemy1_2"); UnityChan = GameObject.Find("UnityChan"); itemgetScript = Itemchange.GetComponent<ItemGet_EquipChange>(); equipsetImage2script = EquipImage2.GetComponent<EquipSetImage2>(); equipsetImage3script = EquipImage3.GetComponent<EquipSetImage3>(); enemyitemScript = Enemy1.GetComponent<EnemyItem>(); actionScript = UnityChan.GetComponent<Action>(); equipimagecolorScript = equipimagecolor.GetComponent<EquipImageColor>(); //アイテムのプレハブをよぶ Soard = (GameObject)Resources.Load("Prefabs/Item_Soard"); Gun = (GameObject)Resources.Load("Prefabs/Item_Gun"); Tue = (GameObject)Resources.Load("Prefabs/Item_Tue"); EquipImageSet1 = Equipimageset1; } void Update() { if (!EquipImageSet1) { if ((equipsetImage2script.EquipImageSet2 == false && equipsetImage3script.EquipImageSet3 == false) || (equipsetImage2script.EquipImageSet2 == true && equipsetImage3script.EquipImageSet3 == true) || (equipsetImage2script.EquipImageSet2 == true && equipsetImage3script.EquipImageSet3 == false)) //装備2,3にアイテムがない場合、または2、3にあり1にない場合に取得したアイテムをスロットに追加 { if (itemgetScript.Soard == true) { itemgetScript.Soard = false; EquipImageSet1 = true; SoardOn = true; sprite = Resources.Load<Sprite>("Soard"); image = this.GetComponent<Image>(); image.sprite = sprite; } else if (itemgetScript.Gun == true) { itemgetScript.Gun = false; EquipImageSet1 = true; GunOn = true; sprite = Resources.Load<Sprite>("Gun"); image = this.GetComponent<Image>(); image.sprite = sprite; } else if (itemgetScript.Tue == true) { itemgetScript.Tue = false; EquipImageSet1 = true; TueOn = true; sprite = Resources.Load<Sprite>("Tue"); image = this.GetComponent<Image>(); image.sprite = sprite; } } } //アイテム削除のさいに使用 if (equipimagecolorScript.weapon == 0) //アイテムスロット1が指定されている場合 { if (EquipImageSet1) { if (MyInput.MyInputKey(KeyCode.V)) { //それぞれのアイテムをドロップ if (SoardOn) { SoardOn = false; Instantiate(Soard, UnityChan.transform.position + new Vector3(0f, 1f, 0f), UnityChan.transform.rotation); } else if (GunOn) { GunOn = false; Instantiate(Gun, UnityChan.transform.position + new Vector3(0f, 1f, 0f), UnityChan.transform.rotation); } else if (TueOn) { TueOn = false; Instantiate(Tue, UnityChan.transform.position + new Vector3(0f, 1f, 0f), UnityChan.transform.rotation); } EquipImageSet1 = false; image = this.GetComponent<Image>(); image.sprite = null; } } } //保持しているデータをOnの方にも記録させる if (Soardon) { SoardOn = true; } else if (Gunon) { GunOn = true; } else if (Tueon) { TueOn = true; } } } コード
**エラーの内容** 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

シーンを移動すると、今いるシーンにあるオブジェクトはすべて破壊されます
解決方法は2つあって、
・シーン移動後に、持っているアイテムを再度生成する
・シーン移動で破壊されない、DontDestroyOnLoadをつける

後者はこんなです

var item = Instantiate(Soard, UnityChan.transform.position + new Vector3(0f, 1f, 0f), UnityChan.transform.rotation); DontDestroyOnLoad(item.gameObject);

投稿2019/11/21 03:56

izmktr

総合スコア2856

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

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

yoshiteru21

2019/11/21 05:24

ご回答ありがとうございます。 生成したものはProject内にあるprefab化したものを取ってきており、シーン移動前の場所には生成されていないはずなのですが、なぜ破壊されていることになっているのでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問