Null reference exception

Hi,

I’ve tried reading up on null reference exceptions, but there must be something fundamental I’m just not getting. I’m trying to follow the Unity “Space Shooter” tutorial and having some trouble since a lot of the content is outdated. Anyway, I’m on the episode where you create a point counter and my code is giving me a null reference exception and I can’t figure out why. Here’s my abbreviated code for the GameController.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameController : MonoBehaviour {

    public GameObject hazard;
    public Vector3 spawnValues;
    public int hazardCount;
    public float spawnWait;
    public float startWait;
    public float waveWait;

    public Text scoreText;
    public int score;

    void Start()
    {
        score = 0;
        UpdateScore();

        StartCoroutine (SpawnWaves());
    }

    IEnumerator SpawnWaves()
    {
        yield return new WaitForSeconds(startWait);
        while (true)
        {
            for (int i = 0; i < hazardCount; i++)
            {
                Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
                Quaternion spawnRotation = Quaternion.identity;
                Instantiate(hazard, spawnPosition, spawnRotation);
                yield return new WaitForSeconds(spawnWait);
            }
            yield return new WaitForSeconds(waveWait);
        }
    }

    public void AddScore(int newScoreValue)
    {
        score += newScoreValue;
        UpdateScore();
    }

    void UpdateScore()
    {
        scoreText.text = "Score: " + score;
    }

}

The part where I get the null reference exception is inside UpdateScore(). Can someone explain why this is happening?

It must be that your ‘scoreText’ is null. Make sure you drag n drop that Text object from the hierarchy onto the script :slight_smile:

Thank you methos5k for your reply! Can you help me decode what you are saying? As a beginner, I’m not sure exactly what you mean and the official Unity tutorials are not very educational, frankly. Mostly they tell you what to do without explaining how things work.

I’d still like to know what I’m doing wrong here, but after this I’ll probably look for a better tutorial so I can actually learn something.

First off, hello and welcome. :slight_smile: Next, I think the most important steps when you first start, are learning the very basics of how to use Unity (Unity interface and essentials probably in the Learning section), and introduction to programming/scripting (also there), so you can have an idea about what scripts mean/do.

I know some of the tutorials need a bit of polish, but as you practice you will be able to easily fix up what’s wrong :slight_smile:

Okay, so here’s what you can try… You have a Text game object for the score in the scene somewhere?
Just locate that, and now click on the gameobject with the script on it. You’ll see the ‘Score Text’ variable on it in the inspector. What you want to do is drag the Text object (score text) from the list of objects in the scene and drop it in the slot on that script. :slight_smile:

Ah, ok! Now I understand. It’s not letting me drag it in- maybe because the game object is GUI text and the script references regular text, but in any case, thank you for explaining that!

Ah okay, that could be it. Simply replace that game object. Make one from “Create → UI → Text” :slight_smile:
Then just put it in the spot you want, and drag the reference there and try the game, again :slight_smile: