- Home /
Timer that can be accessed in multiple scenes
im making a game and have been trying to find a timer script that times you during the main game scene and then displays your time at the end screen that shows up when you lose. I have been looking around and havnt been able to find something that fits what im looking for. can anybody help me out? anything is appreciated.
Answer by aldonaletto · Dec 15, 2011 at 12:18 AM
You can have a static variable to accumulate the time: static variables are created when the program starts, and live while the program is running - even if the script in which the variable is declared is destroyed. The initialization done during declaration only occurs at program start: if you destroy the "owner" object and recreate it after, the variable keeps its last value.
You could do the following:
static var time: float = 0;
function Update(){ time += Time.deltaTime; }
how would i display the value of that variable on the end scene?
You could use a GUIText. Create it with the Create button of the Hierarchy panel, adjust position, size, font, style etc., then set guiText.text to "" - this will make it invisible, since there's no text to show. When you want to show the time, assign the value:
guiText.text = time.ToString("F1"); // display time with 1 decimal place
Your answer
Follow this Question
Related Questions
GUIText not working properly with a timer. 1 Answer
Countdown Timer 1 Answer
Unity freezes when changing GUI.Color.a 1 Answer
Re Enable C# script on camera 1 Answer
Create GUI button in update function? 3 Answers