- Home /
Timer Score display on Game Over Scene?
I finally found away to trigger my game over scene. I attached this script:
function Update () {
if(transform.position.y < -2.6){
GameOver();
}
}
function GameOver(){
Application.LoadLevel("Game Over");
}
to my Star prefab.
But now I cannot seem to get the time to show on the game over scene. Think of it like a high score system. The player's job is to last as long as they can and not let one star pass a certain point or they will lose the game, which will automatically take them to the game over scene. How do I display the player's timer score?
Here is my 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
}
Answer by NoseKills · Jun 08, 2014 at 05:30 AM
You'll have to store the values you want to preserve from one scene to another either in to static variables or GameObjects that you make permanent by calling DontDestroyOnLoad on them. You just need to remeber to reset or null, and/or Destroy() these variables when appropriate to prevent unwanted duplicates and accumulating/wrongly initialized values.
Your answer