Trying to get a score to display, but the UI is giving me error:
Object reference not set to instance of object.
I thought I had this setup properly but not sure why the error.
Script is attached to Canvas object with Text UI object as it’s child.
#pragma strict
import UnityEngine.UI;
var gameState:GameState_Lv01;//Game State Script
var pStats:PlayerStats;//Player Stats Script
var isPaused:boolean = false; //switch for pause.
//UI VARS
var canvas:Canvas; //HUD
var timerTxt:Text; //holds the timerText to be displayed.
var levelComplete:boolean = false;
function Start () {
gameState = GameObject.FindGameObjectWithTag("GameState").GetComponent(GameState_Lv01); //for accessing game stats to display
pStats = GameObject.FindGameObjectWithTag("Player").GetComponent(PlayerStats); //for accessing player attributes to display.
timerTxt = GetComponent(Text); //setup main canvas
timerTxt.text = (System.Math.Round((gameState.timer),0)).ToString();
}
function Update () {
Debug.Log((System.Math.Round((gameState.timer),0)));
Debug.Log(timerTxt.text); // **??? why is this an object not the actual text?**
timerTxt.text = "Score: " + (System.Math.Round((gameState.timer),0));
}
Anyone know why this isn’t giving me the timer?