I’m at the final stages of the tutorial, but now that I’ve tried to add the Score Text to work, I’ve been getting the following error:
NullReferenceException: Object
reference not set to an instance of an
object GameController.UpdateScore ()
(at
Assets/Scripts/GameController.cs:49)
GameController.Start () (at
Assets/Scripts/GameController.cs:21)
I checked in the Inspector, and the scoreText seems appropriately assigned, but when I start playing the game, it gets switched to “None”.
This is my code. What am I missing?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GameController : MonoBehaviour {
public GameObject asteroid;
public int hazardCount; // Number of hazards per wave
public float startWait;
public float spawnWait;
public float waveWait;
private int score;
public Text scoreText;
void Start()
{
scoreText = GetComponent<Text>();
score = 0;
UpdateScore();
}
public void AddScore(int x)
{
score += x;
UpdateScore();
}
void UpdateScore()
{
scoreText.text = "Score: " + score;
}
}