I'm trying to make a timer in Unity but always got error "NullReferenceException: Object reference not set to an instance of an object"
here is my code
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Timer : MonoBehaviour{
public int timeLeft = 5;
public Text countdownText;
// Use this for initialization
void Start()
{
StartCoroutine("LoseTime");
}
// Update is called once per frame
void Update()
{
countdownText.text = ("Time Left = " + timeLeft);
if (timeLeft <= 0)
{
StopCoroutine("LoseTime");
countdownText.text = "Times Up!";
}
}
IEnumerator LoseTime()
{
while (true)
{
yield return new WaitForSeconds(1);
timeLeft--;
}
}
}
please Help