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

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

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

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

Unity

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

Q&A

1回答

830閲覧

シーンを再読み込みするとオブジェクトが出現しなくなる。

santamonica

総合スコア0

C#

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

Unity

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

0グッド

0クリップ

投稿2021/12/16 09:15

前提・実現したいこと

貼ってあるソースコードは盾を出し入れするものです。盾はボタンを押している間しか出現しません。
現在起きている問題は、実行したばかりだと盾を出し入れできるのですが、ゲームオーバーになりリトライした際には盾が出し入れできなくなることです。

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

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. ObjectSet.OnButtonDown (System.Byte controllerId, UnityEngine.XR.MagicLeap.MLInput+Controller+Button button) (at Assets/ObjectSet.cs:33) UnityEngine.XR.MagicLeap.Native.MLThreadDispatch+DispatchPayload2`2[A,B].Dispatch () (at Assets/MagicLeap/Lumin/Utils/MLThreadDispatch.cs:375) UnityEngine.XR.MagicLeap.Native.MLThreadDispatch.DispatchAll () (at Assets/MagicLeap/Lumin/Utils/MLThreadDispatch.cs:185) UnityEngine.XR.MagicLeap.MLDevice.Update () (at Assets/MagicLeap/Lumin/Utils/MLDevice.cs:473)

該当のソースコード

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.XR.MagicLeap; public class ObjectSet : MonoBehaviour { private GameObject shild; void Awake() { shild = GameObject.Find("shild"); } void Start() { MLInput.Start(); MLInput.OnControllerButtonDown += OnButtonDown; MLInput.OnControllerButtonUp += OnButtonUp; shild.SetActive(false); } // Update is called once per frame void Update() { } //バンパーボタンの押下時処理 void OnButtonDown(byte controllerId, MLInput.Controller.Button button) { shild.SetActive(true); } //バンパーボタンの押上時処理 void OnButtonUp(byte controllerId, MLInput.Controller.Button button) { shild.SetActive(false); } }

試したこと

調べてもわかりませんでした。

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

MagicLeap1を使ったアプリを開発していて、開発環境はUnityとC#です。

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

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

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

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

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

guest

回答1

0

csharp

1 MLInput.OnControllerButtonDown += OnButtonDown; 2 MLInput.OnControllerButtonUp += OnButtonUp;

ここでアクションを追加していますが、削除している箇所はありませんよね?

自分はMagicLeapなんて知りませんし、MLInputが何者なのかわかりませんが、MLInputとやらがObjectSetと同じタイミングでDestroyされていれば問題ありませんが、もしシングルトンだったりなど、ObjectSetがDestroyされても残り続けているのであれば、OnControllerButtonDownのアクションを呼び出す→破棄されたObjectSet.OnButtonDownを呼び出す→The object of type 'GameObject' has been destroyed but you are still trying to access it.という流れになるのではないですか。

もしそうであれば、Destroyする際にアクションを削除しておきましょう。

csharp

1 void OnDestory() 2 { 3 MLInput.OnControllerButtonDown -= OnButtonDown; 4 MLInput.OnControllerButtonUp -= OnButtonUp; 5 }

投稿2021/12/16 14:19

katsuko

総合スコア3491

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

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

santamonica

2021/12/17 05:26

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.XR.MagicLeap; public class ObjectSet : MonoBehaviour { private GameObject shild; void Awake() { shild = GameObject.Find("shild"); } void Start() { MLInput.Start(); MLInput.OnControllerButtonDown += OnButtonDown; MLInput.OnControllerButtonUp += OnButtonUp; shild.SetActive(false); } // Update is called once per frame void Update() { } //バンパーボタンの押下時処理 void OnButtonDown(byte controllerId, MLInput.Controller.Button button) { shild.SetActive(true); } //バンパーボタンの押上時処理 void OnButtonUp(byte controllerId, MLInput.Controller.Button button) { shild.SetActive(false); } void OnDestory() { MLInput.OnControllerButtonDown -= OnButtonDown; MLInput.OnControllerButtonUp -= OnButtonUp; } }
santamonica

2021/12/17 05:26

プログラムの修正の仕方はこういうことであっていますか?
katsuko

2021/12/17 12:14

はい。とは言っても、先も言ったとおり自分の手元には環境がないので、とりあえず試してみてください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問