- Home /
Duplicate Question
Why this Error when trying to add a score using the new GUI ?
OK so I'm trying to add a scoring system to my game and as such have been watching the new GUI tutorials. I thought I'd followed things to the letter but when I press play I get the following error -
NullReferenceException: Object reference not set to an instance of an object ScoreManager.Update () (at Assets/Scripts/ScoreManager.cs:25
my script is -
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreManager : MonoBehaviour
{
public static int score; // The player's score.
Text text; // Reference to the Text component.
void Awake ()
{
// 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 = "S c o r e : " + score;
}
}
I have my canvas, with a text component. The text component (ScoreText) has the above script attached to it as described in the tutorial.
Either I've missed something which I just can't see (even after multiple times watching) or I simply don't understand what the guys are trying to tell me.
Any suggestions plz ???
It is saying the text variable isn't assigned. I assume the GetComponent is failing. Double check to make sure both the text component and this script are on the same object.
You can try making the text variable public, commenting out the GetComponent line, and assign it in the inspector to make sure.
Follow this Question
Related Questions
GUI Text Score counter 3 Answers
Problem Displaying Score on GUI text 0 Answers
Guitext hides PHP text on iphone 0 Answers
GUI: Text justification? 2 Answers