Object not set to instance of an object

Hi everyone,

I keep getting this error, even though I don’t know that it’s actually causing problems:

NullReferenceException: Object reference not set to an instance of an object
ScoreLastPage.Main () (at Assets/ScoreLastPage.js:7)

Which is to do with this script, which prints the most recent score and the high score on the “game over” page:

ScoreLastPage.js
    #pragma strict
public static var scoring : ScoreText;
var endText : UI.Text;
var highScoreText : UI.Text;
var highScore : int;

scoring = GameObject.Find("ScoringText").GetComponent.<ScoreText>();

function Awake() {
	DontDestroyOnLoad(transform.gameObject);
}

function Start () {	

	Debug.Log("High Score: " + highScore);
}

function Update(){

	
	endText.text = scoring.printer;

	//BUG: high score doesn't update.
	
	highScoreText.text = "High Score: " + highScore;

	if (parseInt(scoring.go.flips) > highScore) {
		PlayerPrefs.SetInt("highScore1", scoring.go.flips);
		PlayerPrefs.Save();
		highScore = PlayerPrefs.GetInt("highScore1");
		
	}
	else {
		highScore = PlayerPrefs.GetInt("highScore1");
	}
	highScoreText.text = "High Score: " + highScore;

}

It draws from this script to get the current score (ScoreText.js):

#pragma strict

var go : pizza;
var text : UI.Text;
public static var printer : String;

function Awake() {
	DontDestroyOnLoad(transform.gameObject);
}

function Start () {
	go = GameObject.Find("pizza-small").GetComponent(pizza);

}

function Update () {
	printer = go.flips.ToString();
	text.text = "Score: " + printer;
}

That script takes the tracked number of flips from a script called “pizza.js”, and prints the score live, during the gameplay. Also, ScoreLastPage.Js has issues with my playerprefs and how I save them, so my questions are:

  • How can I get rid of my aforementioned error?

  • If the player gets a new score, that is overwritten as the high score, even if the value is not higher than the previous high score. For instance, if the high score is 4, and I get 1 point, the high score is set to 0.

You should always initiate variables in the start or awake functions.

The reason you’re getting that error is because the game object you’re trying to reference in your script is null.

ScoreLastPage.js
#pragma strict
     public static var scoring : ScoreText;
     var endText : UI.Text;
     var highScoreText : UI.Text;
     var highScore : int;
     
     function Awake()
{
    DontDestroyOnLoad(transform.gameObject);
}

function Start()
{
    scoring = GameObject.Find("ScoringText").GetComponent.<ScoreText>();

    if (scoring == null)
    {
        Debug.Log("Unable to find ScoringText");
        return;
    }

        Debug.Log("High Score: " + highScore);
}

function Update()
{
    if (scoring != null)
    {
        endText.text = scoring.printer;

        highScoreText.text = "High Score: " + highScore;

        if (parseInt(scoring.go.flips) > highScore)
        {
            PlayerPrefs.SetInt("highScore1", scoring.go.flips);
            PlayerPrefs.Save();
            highScore = PlayerPrefs.GetInt("highScore1");
        }
        else
            highScore = PlayerPrefs.GetInt("highScore1");

        highScoreText.text = "High Score: " + highScore;
    }
}

Alright I solved it, and I’ll leave this for future reference of anyone searching:

I just redid the scoring code completely and made a “Game Manager” prefab with a game manager script on it, made the score a static variable, and passed everything from the game manager script.