Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Good Day Everyone, I am working on a script and it works really well, but there is just this one error that i just cannot figure out. What am i overlooking? I will post the error and code below.

Here is the error:

UnassignedReferenceException: The variable gameOverScore of Score has not been assigned.
You probably need to assign the gameOverScore variable of the Score script in the inspector.
Score.Start () (at Assets/2dspaceshooter/Scripts/Score.js:10)

Here is the script:

JavaScript
#pragma strict

var gameOverScore:GUIText;
var gameGUI:GameObject;

private var score:int = 0;
private var isGameOver = false;

function Start () {
gameOverScore.guiText.enabled = false;
guiText.text = "Score: " + score.ToString();
}

function addScore () {
    if(!isGameOver){
        score += 10;
        guiText.text = "Score: " + score.ToString();
    }
}

function doGameOver () {
    isGameOver = true;
    gameGUI.SetActive(false);
    guiText.text = null;
    gameOverScore.guiText.enabled = true;
    gameOverScore.guiText.text = "Score: "+score;
}
Posted
Updated 29-Dec-14 9:07am
v3

1 solution

This is simple: you never initialize gameOverScore in your code. When you try get calculate the property gameOverScore.guiText, you try to dereference object by calling its property guiText, and it throws the exception because, at this moment, the reference gameOverScore does not point to any GUIText object. Initialization could be something like
JavaScript
gameOverScore = new GUIText( /* ...  */ );


[EDIT]

Your conceptual mistake is thinking that you are doing assignment by this code
JavaScript
gameOverScore.guiText = someValue; // no assignment to gameOverScore!
Instead, you assign the value to the property "guiTest", which is equivalent to access to the element of associative container by string index:
JavaScript
gameOverScore["guiText"] = someValue; // same as above
// naturally gameOverScore should exist before doing it

Note that understanding of properties as elements of associative containers is in a deep nature of Javascript, one of the most fundamental ideas of this language.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900