Object Reference not set to an instance of object / printing a variable from another scene

Hello!

I’m essentially trying to get the score (# of flips) from this script:

 #pragma strict
 
 var flips = 0;
 
 var facingUp = true;
 
 function Update() {
 
     if (facingUp) {
         if (Vector3.Dot(Vector3.up, transform.up) < -0.75) {
             facingUp = false;
             flips++;
         }
     }
     else {
         if (Vector3.Dot(Vector3.up, transform.up) > 0.75) {
             facingUp = true;
             flips++;
         }
     }

     Debug.Log(flips);
     
 }

and print it on the screen during the “game over” scene, but I don’t know how to get the variable to carry over. On top of that, I’m getting an error with my text on the “game over” scene:

“Object reference not set to an instance of an object”, which appears to be due to my ToString() method in the following code:

#pragma strict

var go : pizza;
var text : UI.Text;
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;
}

I have tried to use DontDestroyOnLoad in the first script, but I assume that it doesn’t work since there’s an error with my text on the second script? I’m really unsure how to go about it, any help is appreciated. Thanks!

The error “Object reference not set to an instance of an object” does not have anything to do with calling int.ToString() as part of the expression go.flips.ToString();. The only possible object reference in that expression is go, meaning the expression go.flips fails because go is set to null.

But let’s step back a little to get an overview. You seem to have a pizza object in your game scene and then some UI presentation object in your gameover scene. It sounds like you want the pizza object to carry over into the gameover scene but you are not telling the pizza object to DontDestroyOnLoad. You are telling the UI presentation object that. If you wanted your pizza object to carry over to the next scene, you should move the call of DontDestroyOnLoad to the pizza script.

However, it could be easier to just declare a static variable which is accessible regardless if the pizza object lives or not.

pizza.js

#pragma strict
  
static var flips = 0;      
var facingUp = true;

function Awake() {
    flips = 0; // Since it is a static var, we want to reset it every time we play or it will keep adding between games
}

function Update() {      
    if (facingUp) {
        if (Vector3.Dot(Vector3.up, transform.up) < -0.75) {
            facingUp = false;
            flips++;
        }
    }
    else {
        if (Vector3.Dot(Vector3.up, transform.up) > 0.75) {
            facingUp = true;
            flips++;
        }
    }     
    Debug.Log(flips);
}

pizzaPresenter.js

#pragma strict
 
var text : UI.Text;
 
function Update () {
    text.text = "Score: " + pizza.flips.ToString();
}