- Home /
Display Time At Game Over Scene
I have this time 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;
}
Stars all falling and the player's job is to destroy the stars by touching them and they cannot let one pass or they will lose the game. The score system is basically the time; how long they last. But, I'm having trouble with creating a game Over trigger and displaying the player's highest time in the Game Over scene. How do I do it?
Answer by Jeff-Kesselman · Jun 05, 2014 at 09:58 PM
Move var guiTime to outside of your functions to make it a global
make an Update() function that checks guiTime and, if guiTime is > your time end, load the score scene.
be aware that score will be reset when the scene changes unless you make it a global static
Do I just move this line?
var guiTime = Time.time - startTime;
No, just move the declaration
var guiTime;
That makes it visible to multiple methods.
THEN change the use in OnGui so it doesn't redefine it:
guiTime = Time.time - startTime;
FInally, write an Update method that checks it.
Alternately, as noted below, you could us e a global boolean, set it in your time code and check it in update. Same difference.
Answer by Tekksin · Jun 05, 2014 at 10:42 PM
Just wrap it in a boolean:
var gameOver : boolean;
function Update(){
if(!gameOver){
//do everything you listed in your script
} else{
//use the vars in (!gameOver) to get the time amounts to post here, but be sure not to include the accruing code.
//show the information of game over.
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613150902im_/https://answers.unity.com/themes/thub/images/avi.jpg)