Time Score?

Okay, this is the only thing that is hindering the completion of my game. I have a timer script:

#pragma strict
private var startTime : float;
var textTime : String;
//First define two variables. One private and one public variable. Set the first variable to be a float. 
//Use for textTime a string. 
function Start() {
startTime = Time.time;
}
function OnGUI () {
var guiTime = Time.time - startTime; 
//The gui-Time is the difference between the actual time and the start time.
var minutes : int = guiTime / 60; //Divide the guiTime by sixty to get the minutes.
var seconds : int = guiTime % 60;//Use the euclidean division for the seconds.
var fraction : int = (guiTime * 100) % 100;
 
 textTime = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction); 
//text.Time is the time that will be displayed.
 GetComponent(GUIText).text = textTime; 
}
 
var bestScore = 0;
var playerTime = 0;
//When the race is completed;
bestScore = playerTime;
//Reset playerTime to zero
//Display the best score(Score to beat)
//If the next drivers playerTime is higher than the previous bestScore
if(playerTime > bestScore){
  //Display the new best score.
 //Reset playerTime to zero
}

The player’s job in my game is to tap each of the spawning stars to destroy them, and if they let one pass a certain point they lose the game. The Timer is there to basically see how long each player can last.

I’ve tried, but I cannot seem to save the timer score and display on the GameOver Scene. How do I go about this? Could you please explain in a step by step way.

When you change scenes, all old objects of your scene will be destroyed. In order to keep your objects or scripts, you will have to use a DontDestroyOnLoad - method in your class.

The data that you store in that class, can be accessed from another class. The easiest way to do this, is by creating a public static variable.

  • Create PersistentGameData.js
  • Add the script to an empty gameobject
  • Add DontDestroyOnLoad in the Awake method
  • Create a public static var gameScore.
  • Set it by using PersistentGameData.gameScore = bestScore
  • You can now access this variable anywhere, anytime, even after the scene changed.

Good luck!