プログラミング初心者です
以下のコードでタイマーを実装しようとしたのですが、
NullReferenceException: Object reference not set to an instance of an object
Timer.Update () (at Assets/Timer.cs:24)
のようにエラーが出て動きませんでした。
どなたか対処法を教えていただくと助かります。
バージョン
Unity 2021.3.2f1 personal
追記
該当するコードはこちらで全文なのですが、こちらのスクリプトをUnityでアタッチした際に動かなくなるという質問です。
ヒエラルキー内でMain>Canvas>TimerTextのようにUIオブジェクトを作成し、
TimerTextにスクリプトをアタッチしました。
アセットストアにてStandard Assets (for Unity 2018.4)を使用しているのですが、
Assets>SrandardAssets>Utility>EventSystemChecker.csにて、
18行目 obj.AddComponent<StandaloneInputModule>().forceModuleActive = true;に旧型式ですとの表記がありました。
関係ないかもしれないですが追記しておきます。
該当箇所のスクリーンショットを追加しました。
Timer.cs
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class Timer : MonoBehaviour 7{ 8 public static float time; 9 // Start is called before the first frame update 10 void Start() 11 { 12 time = 0; 13 } 14 15 // Update is called once per frame 16 void Update() 17 { 18 if (GoalJudge.goal == false) 19 { 20 time += Time.deltaTime; 21 } 22 int t = Mathf.FloorToInt(time); 23 Text uiText = GetComponent<Text>(); 24 uiText.text ="Time:" + t; 25 } 26} 27
EventSystemChecker.cs
C#
1using System; 2using System.Collections; 3using UnityEngine; 4using UnityEngine.EventSystems; 5 6public class EventSystemChecker : MonoBehaviour 7{ 8 //public GameObject eventSystem; 9 10 // Use this for initialization 11 void Awake () 12 { 13 if(!FindObjectOfType<EventSystem>()) 14 { 15 //Instantiate(eventSystem); 16 GameObject obj = new GameObject("EventSystem"); 17 obj.AddComponent<EventSystem>(); 18 obj.AddComponent<StandaloneInputModule>().forceModuleActive = true; 19 } 20 } 21}
回答1件
あなたの回答
tips
プレビュー