- Home /
Object reference not set to the instance of an object
Hey all, So, today I've been trying to program a timer for a game I'm making. In the game, the timer counts soun from 60 seconds to 0, and at 0, the game ends. Just as a placeholder for the moment ( because I can't seem to get it to count down in seconds) I have set the timer value to 60, and applied the scrit for the timer to a text so the value is seen visibly counting down. Here is the script I have:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Timer : MonoBehaviour {
public int timer;
public Text timerText;
public GameObject Timer
// Use this for initialization
void Start () {
timer = 600;
}
// Update is called once per frame
void Update () {
timerText.text = timer.ToString (Timer);
timer -= 1;
}
}
The error seems to happen line 18. It is indeed, rather annoying. Any and all help and pointers would be greatly appreciated, thanks :)
Answer by SoloDeveloper · Dec 29, 2015 at 05:21 AM
Check in Editor if the
public Text timerText;
is emptyAlso change
timerText.text = timer.ToString (Timer);
totimerText.text = "" + timer;
Also change
timer -= 1;
to `timer -= Time.deltaTime;
And you should round the timer value or cast it to int as long as you don't want fractions of seconds to be displayed in timerText.text;
Your answer
![](https://koobas.hobune.stream/wayback/20220612054003im_/https://answers.unity.com/themes/thub/images/avi.jpg)