I’m simply trying to change the text of a UI text box, that is located in UICanvas>HintBody in my hierachy.
Below is my code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class LoadingScreenLogic : MonoBehaviour {
Text HintBody;
void Start () {
HintBody = GetComponent<Text>();
HintBody.text = "It works!";
if ((PlayerPrefs.GetString("SceneToLoad") == "") || (PlayerPrefs.GetString("SceneToLoad") == "InitScreen")) {
Application.targetFrameRate = 120;
PlayerPrefs.SetString("SceneToLoad", "InitScreen");
}
}
void Update () {
Debug.Log(PlayerPrefs.GetString("SceneToLoad"));
if(Application.GetStreamProgressForLevel(PlayerPrefs.GetString("SceneToLoad")) ==1){
Application.LoadLevel(PlayerPrefs.GetString("SceneToLoad"));
}
}
}
However, whenever I try to change the text of the text box this way, this appears in the log and nothing happens:
NullReferenceException: Object reference not set to an instance of an object
LoadingScreenLogic.Start () (at Assets/Assets/Scripts/LoadingScreenLogic.cs:11)
I don’t know where to go from here. I’ve tried making Text HintBody public (and private), but it doesn’t work. I’m only really getting started with C#.