First, teh script that counts the score
#pragma strict
public var NewScore : float;
public var Score : float;
function Start(){
Score = Time.time;
}
function OnGUI () {
GUI.Label (Rect (10, 10, 500, 20), "Score : " + NewScore);
}
function Update () {
NewScore = Time.time - Score;
}
In the same scene is a separate null that will save the data
#pragma strict
public var NewScore2 : float;
function Awake(){
DontDestroyOnLoad(this);
}
function Update () {
NewScore2 = GetComponent(GUI_Demo).NewScore;
}
And on the scene after that, you display teh score.
#pragma strict
public var Finalscore : float;
function Start(){
Finalscore = GetComponent(DontDestroy).NewScore2;
}
function OnGUI () {
GUI.Label (Rect (10, 10, 200, 20), "Your score is : " + Finalscore);
GUI.Label (Rect (10, 30, 200, 20), "Press space to play again");
}
function Update () {
if (Input.GetKey (KeyCode.Space)){
Application.LoadLevel ("demo");
}
}
Problem. It keeps saying that
NullReferenceException: Object reference not set to an instance of an object
DontDestroy.Update () (at Assets/scripts/DontDestroy.js:9)