前提
unityでUIのtextに指定した文字を表示させるシステムを作成しています。
その際に以下のエラーメッセージが発生しました。
実現したいこと
TargetLogicalTextとTargetInputTextに指定した文字を表示させたい。
発生している問題・エラーメッセージ
NullReferenceException: Object reference not set to an instance of an object TargetWord.Start () (at Assets/TargetWord.cs:25)
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class TargetWord : MonoBehaviour { 7 8 private WordMatcher matcher; 9 private InputField input_field; 10 // 日本語表示テキスト 11 private Text Logical_Text; 12 // ローマ字表示テキスト 13 private Text Input_Text; 14 private string[] qJ = {"もんだい", "てすと", "たいぴんぐ"}; 15 private string[] qR = {"mondai", "tesuto", "taipingu"}; 16 // 問題番号 17 private int numberOfQuestion; 18 private string nQJ; 19 private string nQR; 20 private int indexOfString; 21 private string correctString; 22 23 void Start () { 24 input_field = this.GetComponentInChildren<InputField>(); 25 Logical_Text = transform.Find("TargetWord/TargetLogicalText").GetComponent<Text>(); 26 Input_Text = transform.Find("TargetWord/TargetInputText").GetComponent<Text>(); 27 28 // 問題数内でランダムに選ぶ 29 numberOfQuestion = Random.Range(0, qJ.Length); 30 31 string target_word = "ありがとうございます"; 32 matcher = new WordMatcher(target_word); 33 34 // 選択した問題をテキストUIにセット 35 nQJ = qJ[numberOfQuestion]; 36 nQR = qR[numberOfQuestion]; 37 Logical_Text.text = nQJ; 38 Input_Text.text = nQR; 39 40 41 OutputQ(); 42 } 43 44 /// <summary> 45 /// InputFiled変更時のコールバック関数 46 /// </summary> 47 public void onInput(string text) { 48 if (text.Length == 0) { 49 return; 50 } 51 52 string input_char = text.Substring(text.Length - 1, 1); 53 bool result = matcher.matching(input_char); 54 55 // 入力内容と一致しなかった場合は戻す 56 if (!result) { 57 input_field.text = matcher.getInputtedString(); 58 } 59 } 60 void OutputQ() { 61 // テキストUIを初期化する 62 Logical_Text.text = ""; 63 Input_Text.text = ""; 64 65 // 正解した文字列を初期化 66 correctString = ""; 67 // 文字の位置を0番目に戻す 68 indexOfString = 0; 69 70 // 問題数内でランダムに選ぶ 71 numberOfQuestion = Random.Range(0, qJ.Length); 72 73 // 選択した問題をテキストUIにセット 74 nQJ = qJ[numberOfQuestion]; 75 nQR = qR[numberOfQuestion]; 76 Logical_Text.text = nQJ; 77 Input_Text.text = nQR; 78} 79}
試したこと
参照するtextの名前変更など
nullになっているというのが分かったが、何をすればいいのかわかっていない。
補足情報(FW/ツールのバージョンなど)
テキストの表示部分の原因についてのみ触れていただけるとありがたいです。
回答2件
あなたの回答
tips
プレビュー