i am trying to display a timer in unity. I get always this error: NullReferenceException: Object reference not set to an instance of an object. If anyone can help me it will be really nice <3
public class PlayerScore : MonoBehaviour
{
private float timeLeft = 120f;
public float playerScore=0;
public GameObject timeLeftUI;
public GameObject playerScoreUI;
private void Start()
{
timeLeftUI.gameObject.GetComponent<Text>().text = ("Time: " + timeLeft).ToString();
}
// Update is called once per frame
void Update()
{
timeLeft -= Time.deltaTime;
timeLeftUI.gameObject.GetComponent<Text>().text = ("Time: " + timeLeft).ToString();
//Debug.Log(timeLeft);
if (timeLeft < 0.1) //si le time est plus petit que 0 alors la partie se termine
{
//partie terminé
}
}
void CountScore()
{
playerScore = playerScore + (timeLeft * 10);
}
}```