- Home /
Ive been trying to get a score counter to work for 2 weeks please help.
I have no idea what is going on with this game it is a very simple 2d game with cubes that spawn in a plane (enemy's) you have to collect all of the gold cubes to win. All i'm trying to do is make it so when I collect one of those cubes my score counter goes up by one I keep getting this error NullReferenceException: Object reference not set to an instance of an object ScoreCounter.Update () (at Assets/ScoreCounter.cs:26)
I don't understand what is wrong with my script it should just be a very simple script.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreCounter : 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 = "Score:" + score;
}
}
most likely because the object which this script is attached to does not have a Text
component.
check for null
and print some message on the console in Awake()
to find out if that's the case
I tried to use the Debug.WriteLine("")
but it said it did not recognise it so i couldn't print anything to the console the text is attached to the canvas and comes up with "Score:0" when I start the game but does not add a point when i destroy the point gameObject. This is making me very upset im in the middle of my game creation class cussing and carrying on because its been 2 weeks ive been stuck on this
Your answer
Follow this Question
Related Questions
Scoring System 3 Answers
How to make a scoreboard? 0 Answers
gamemanager: monobehaviour, scriptable objects, or static class 1 Answer
Highscore with points and time 1 Answer