実現したいこと
【実現したいこと】
UnityのC#で、吹き出しを表示し会話する機能を作成しています。
以下のような動作を実現したいです。よろしくお願いします。
【動作内容】
メイン画面のボタンを押す
↓
メイン画面の上に追加表示で、吹き出しを表示し「あなたの名前は?」と聞いてくる
↓
吹き出しの上にさらに追加表示で、名前を入力するダイアログを表示する。
名前を入力しボタンを押してダイアログを閉じる。
↓
吹き出しに戻り、入力した名前を表示する。
Endボタンを押して吹き出しを閉じる。
↓
メイン画面に戻る。
【困っていること】
Sceneの追加表示での画面遷移がうまく行かず困っています。
1回目は正常に動作するのですが、2回目を行うと以下の(発生している問題・エラーメッセージに記述した)エラーが表示されます。
1回目の動作時のデバッグです。this(TalkSceneのことだと思います)は破棄されていないのですが
発生している問題・エラーメッセージ
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.GetComponentInChildren[Text] (Boolean includeInactive) (at C:/buildslave/unity/build/artifacts/generated/bindings_old/common/Core/GameObjectBindings.gen.cs:79) UnityEngine.GameObject.GetComponentInChildren[Text] () (at C:/buildslave/unity/build/artifacts/generated/bindings_old/common/Core/GameObjectBindings.gen.cs:74) Talk.OnSceneUnloaded (Scene scene) (at Assets/vytTest/vytScripts/Talk.cs:38) UnityEngine.SceneManagement.SceneManager.Internal_SceneUnloaded (Scene scene) (at C:/buildslave/unity/build/artifacts/generated/bindings_old/common/Core/SceneManagerBindings.gen.cs:257)
該当のソースコード
C#
1using System; 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using UnityEngine.SceneManagement; 6using UnityEngine.UI; 7 8public class Main : MonoBehaviour 9{ 10 void Start() 11 { 12 //Mainボタン 13 GameObject mainButton = GameObject.Find("MainCanvas/Button"); 14 Button button = mainButton.GetComponent<Button>(); 15 button.onClick.AddListener(() => ButtonClick()); 16 } 17 18 void Update() 19 { 20 } 21 22 public void ButtonClick() 23 { 24 //TalkSceneに遷移する 25 SceneManager.LoadSceneAsync("TalkScene", LoadSceneMode.Additive); 26 } 27}
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5using UnityEngine.UI; 6 7public class Talk : MonoBehaviour 8{ 9 GameObject talkText; 10 11 GameObject endButton; 12 13 const string ANSWER_KEY = "Answer"; 14 15 void Start() 16 { 17 //文字を表示するテキスト 18 talkText = GameObject.Find("TalkCanvas/TalkText"); 19 20 //EndボタンにClickListenerを設定する 21 endButton = GameObject.Find("TalkCanvas/EndButton"); 22 Button button = endButton.GetComponent<Button>(); 23 button.onClick.AddListener(() => EndButtonClick()); 24 endButton.SetActive(false); 25 26 //DialogSceneが破棄されたことを検知する 27 SceneManager.sceneUnloaded += OnSceneUnloaded; 28 29 ShowWord(); 30 } 31 32 void OnSceneUnloaded(Scene scene) 33 { 34 if (scene.name == "DialogScene") 35 { 36 //回答を表示する 37 string word = "こんにちは、" + PlayerPrefs.GetString(ANSWER_KEY, "NotSave") + "さん。"; 38 talkText.GetComponentInChildren<Text>().text = word; //←★2回目の時にここでエラーで落ちる 39 40 //Endボタンを表示する 41 endButton.SetActive(true); 42 } 43 } 44 45 public void EndButtonClick() 46 { 47 SceneManager.UnloadSceneAsync("TalkScene"); 48 } 49 50 //文字を表示する 51 public void ShowWord() 52 { 53 talkText.GetComponentInChildren<Text>().text = "あなたの名前は?"; 54 55 Invoke("ShowDialog", 0.5f); 56 } 57 58 //ダイアログを表示する 59 public void ShowDialog() 60 { 61 SceneManager.LoadSceneAsync("DialogScene", LoadSceneMode.Additive); 62 } 63}
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5using UnityEngine.UI; 6 7public class Dialog : MonoBehaviour 8{ 9 GameObject dialogInputField; 10 11 GameObject yesButton; 12 13 const string ANSWER_KEY = "Answer"; 14 15 void Start() 16 { 17 //回答を入力テキスト 18 dialogInputField = GameObject.Find("DialogCanvas/DialogInputField"); 19 20 //Yesボタン 21 yesButton = GameObject.Find("DialogCanvas/YesButton"); 22 23 //YesボタンにClickListenerを設定する 24 Button button = yesButton.GetComponent<Button>(); 25 button.onClick.AddListener(() => YesButtonClick()); 26 } 27 28 void Update() 29 { 30 } 31 32 public void YesButtonClick() 33 { 34 string inputText = dialogInputField.GetComponentInChildren<InputField>().text; 35 36 //回答を保存する 37 PlayerPrefs.SetString(ANSWER_KEY, inputText); 38 PlayerPrefs.Save(); 39 40 //ダイアログを閉じる 41 SceneManager.UnloadSceneAsync("DialogScene"); 42 } 43}
補足情報
Unityのバージョンは「Unity 2018.1.5f1 Personal(64bit)」を使用しています。
回答1件
あなたの回答
tips
プレビュー