Hi Nina,
(This was posted in Udemy but no reply.)
I cannot understand why I get this error. Can you check the entire file for me?
NullReferenceException: Object reference not set to an instance of an object StarDisplay.UpdateDisplay () (at Assets/Scripts/StarDisplay.cs:19)
StarDisplay.Start () (at Assets/Scripts/StarDisplay.cs:14)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class StarDisplay : MonoBehaviour
{
[SerializeField] int stars = 100;
Text starText;
void Start()
{
starText = GetComponent<Text>();
UpdateDisplay();
}
private void UpdateDisplay()
{
starText.text = starText.ToString();
}
public void AddStars(int amount)
{
stars = stars + amount;
UpdateDisplay();
}
public bool HaveEnoughStars(int amount)
{
return stars >= amount;
}
public void SpendStars(int amount)
{
if (stars >= amount)
{
stars = stars - amount;
UpdateDisplay();
}
}
}
Someone gave
starText.text = stars.ToString();
and thought it might work. I still have the same error.