I’m trying to create a basic timer to display on a text component but for some reason I am getting a null reference and I’m not too sure why. I have set a break point on the line responsible and it is telling me that the text component is populated.
Here is my code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TimerDisplay : MonoBehaviour {
public float gameTime;
Text time;
// Use this for initialization
void Start () {
gameTime = 0.00f;
time = gameObject.GetComponent<Text>();
time.text = "00";
}
// Update is called once per frame
void Update () {
gameTime += Time.deltaTime;
time.text = Mathf.FloorToInt(gameTime).ToString();
}
}
and I have attached this to a text prefab in game
I’m very new to Unity and I’m not too sure how to approach this. Thanks in advance for any help.