Q&A
前提・実現したいこと
Unityでボタンを押したら簡単な式(X+Y)が出てきて、InputField内にその答えを打ち込む三ボタンを押すとまた新しい式が出てくるというのを制作しています、、そこでボタンを押すと出てくるエラーがありました。
発生している問題・エラーメッセージ
NullReferenceException: Object reference not set to an instance of an object Botan.OnClick () (at Assets/Botan.cs:20) UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:66) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:108) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class Botan : MonoBehaviour { 7 public GameControlloer gamecontroller; 8 public InputField inputField; 9 10 int z; 11 12 13 14 public void OnClick () { 15 Debug.Log(3); 16 17 inputField = inputField.GetComponent<InputField>(); 18 string x = inputField.text; 19 z = int.Parse(x); 20 int banswer = gamecontroller.answer; 21 Debug.Log(3); 22 int segyo; 23 segyo = gamecontroller.seigyo; 24 if (z == banswer) { 25 segyo++; 26 Debug.Log(2); 27 28 29 } 30 } 31} 32 33
using UnityEngine; using System.Collections; using UnityEngine.UI; public class GameControlloer : MonoBehaviour { InputField inputField; public Text text; private Text targetText; public InputManager inputmanager; // Start is called before the first frame update public int one; public int two; public int answer; string hyouzi; int z; public int seigyo; void Start() { int monndaisuu = 0; while (monndaisuu == 10) { Debug.Log("成功!"); break;} do { one = Random.Range(1, 11); two = Random.Range(1, 11); answer = one + two; Debug.Log(one + "+" + two + "+" + "=" + answer); hyouzi = one + "+" + two + "="; text.text = hyouzi; Debug.Log(1); monndaisuu++; seigyo++; } while (seigyo%2==0); } }
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
こちらの質問が複数のユーザーから「過去の低評価」という指摘を受けました。
2020/11/02 07:35
2020/11/02 07:56
2020/11/20 07:20