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!
Answer by Statement · Oct 10, 2015 at 02:58 PM
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();
}
I was able to get the score to on the game over scene, yet I still get the error. The instance of
scoring = GameObject.Find("text").GetComponent.<ScoreText>();
for the "Game Over scene" gives the error, but is still able to print the score using the data from it.
Everything appears to function, but I'm still getting the error.
$$anonymous$$y pizza.js is currently the same as what you posted. $$anonymous$$y Last page script is:
#pragma strict
var scoring : ScoreText;
var endText : UI.Text;
scoring = GameObject.Find("text").GetComponent.<ScoreText>();
function Start () {
}
function Update(){
endText.text = scoring.printer;
}
$$anonymous$$eanwhile, the script that prints the score live during the game itself is:
#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;
}
Your answer
Follow this Question
Related Questions
Complicated level change problem 1 Answer
Saving and loading an int variable on Android 1 Answer
How to make a global variable in Unity? 5 Answers