Store fixed time in variable and output on GUI to gameover screen?

Hey everyone,

I’m trying to output the length of time the game ran as the players score on the ‘game-over’ screen.

so far I have the code

void OnGUI()
    {
        GUI.Label(new Rect(10, 10, 150, 30), "Seconds Alive: " + (int)(Time.fixedTime)); // Multiply playerScore by whatever you want to make for (seconds played * x)
    }

This code works during the game, outputting the seconds the player has been alive.

for the exist screen I have…

void OnGUI()
    {
        GUI.Label(new Rect(Screen.width / 2 - 40, 50, 80, 30), "YOU DIED");

        GUI.Label(new Rect(Screen.width / 2 - 40, 300, 80, 30), "You survived for : " + Time.fixedTime);
        if (GUI.Button(new Rect(Screen.width / 2 -30, 350, 60, 30), "Retry?"))
        {
            Application.LoadLevel(0);
        }
    }

However it just counts up the time the current screen has been loaded.

Anybody have any ideas?

The variables deltaTime and fixedDeltaTime give you the amount of time the last render took to process. Take a look at the Time class, I’m sure there are a few ways of doing that. There are a couple of variables there you could use, like ‘realtimesincestartup’ and ‘realtimesincelevelload’. You could either use these variables directly or, if the time they start running is not appropriate for your game or you want a way to pause the timer, just create your own timer using deltaTime. For example

var timer : float = 0;

function Update(){
   //your code
   timer += Time.deltaTime;
}

and use this ‘timer’ on your OnGUI.