- Home /
Timer gets executed in the wrong scene
Hello.
I started using Unity 2 days ago and have created a small "Break Out" type of game. I now have a problem with a timer script: Apparently it gets executed in the wrong scene or globally for some reason.
For now, I have 2 scenes: One game scene which contains the timer. It is displayed on the screen as a GUI text and counts down from 2:00 minutes to 0:00. The clockscript (see below) is attached to a Game Object ("Game Object Clock") that also includes the GUItext component.
I added another scene to the game, which is a splash title screen. It has a "click to play" GUI button which leads to the game scene.
For some reason, the clockscript seems to run immediately after the game starts, i.e. during the title screen. Once I click the play button, the game scene is loaded with a certain amount of time already substracted from the countdown (= exactly as long as I had the title screen running before clicking the button).
Information:
the script is not attached to any object in the title screen scene.
the "Game Object Clock" is not part of the hierarchy in the title screen scene at all.
the debug log message from the clock script does appear during the game scene just fine, but it does NOT appear during the title screen. This leads me to the conclusion that only part of the script is running prematurely. Or maybe not...
Stuff that has not worked to correct the issue:
replacing the Start () function with a different function name or leaving the Start () function empty.
applying the clockscript to a Game Object or the Main Camera in the title screen scene and setting it to "pause"
trying to delay the script in "execution order" (clock script inspector settings)
How can I keep the clock from invisibly ticking away during the title screen and make it start at exactly 2:00 only when the actual game level starts? Or is there maybe a way to reset it upon loading the right level?
It's probably something really easy and trivial, but nothing I try seems to work or even affect the script in any way. Any help is appreciated.
clockscript code:
var isPaused : boolean = false;
var startTime : float; //(in seconds)
var timeRemaining : float; //(in seconds)
function Start() {
startTime = 120.0;
}
function Update() {
if (!isPaused)
{
DoCountdown();
}
}
function DoCountdown() {
timeRemaining = startTime - Time.time;
Debug.Log("time remaining = " + timeRemaining);
if (timeRemaining < 0)
{
timeRemaining = 0;
isPaused = true;
TimeIsUp();
}
ShowTime();
}
function PauseClock() {
isPaused = true;
}
function UnpauseClock() {
isPaused = false;
}
function ShowTime() {
var minutes : int;
var seconds : int;
var timeStr : String;
minutes = timeRemaining/60;
seconds = timeRemaining % 60;
timeStr = minutes.ToString() + ":";
timeStr += seconds.ToString("D2");
guiText.text = timeStr; //display the time to the GUI
}
function TimeIsUp() {
Debug.Log("Time is up!");
}
Answer by PiffPaff · Feb 20, 2012 at 03:21 AM
Hey PiffPaff, I figured this out for you:
Time.time
needs to be replaced with Time.timeSinceLevelLoad
Your answer
Follow this Question
Related Questions
Need help with switch case in order/timed 0 Answers
Timer that can be accessed in multiple scenes 1 Answer
Movement in my terrain ? 2 Answers
Scene Audio Script Error 2 Answers
Adding commands to buttons? 1 Answer