0

I keep getting this error on a score counter script for a game that I am makingthe score counter works but this error is just annoying and I dont want ot publish it with the errors stacking up.

Error:

NullReferenceException: Object reference not set to an instance of an object
ScoreCounter.Update () (at Assets/ScoreCounter.cs:26)

Script:

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

public class ScoreCounter : MonoBehaviour
{
public static int score;        // The player's score.


public Text text;                      // Reference to the Text component.

void Start ()
{
    // Set up the reference.
    //text = GetComponent <Text> ();

    // Reset the score.
    score = 0;
}


void Update ()
{
    // Set the displayed text to be the word "Score" followed by the score value.

    text.text = "Score:" + score;
}
}
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Parker
  • 1

1 Answers1

0

From the error message it's very clear that your text object is never instantiated.

I don't know enough about your use-case to recommend where it should be instantiated, but inside of Start() looks like a good place.

Cheers

Luc Morin
  • 5,302
  • 20
  • 39