- Home /
Need help with delaying a timer (javascript)
Ok I currently have my scenes running like this.
Main Menu
|
Player Select
|
Level 1
In the Level 1, I have a script (Level1InfoScript) that has a timer that countsdown and when it hits zero it will end the level (haven't written that part yet). My problem is that the timer starts counting down right when the game is launched on the main menu, instead of waiting until Level1 is loaded to countdown. Making it look like this.
Main Menu(this is when the timer starts)
|
Player Select
|
Level1 (this is where the timer is suppose to start)
I'm very new to programming and stuff, so any help or advice would be great. I looked on here before asking, and found a somewhat similar question, but the advice given in that wasn't working, so that why I am now here myself.
Here is the code. I got most of the code from a Unity Beginners Guide book and building from there. I have omitted stuff that doesn't pertain to the timer itself.
var isPaused : boolean = false;
var startingTime : float; // This is in seconds
var timeRemain : float; // Also in seconds
function Start()
{
startingTime = 20.0;
}
function Update ()
{
if(!isPaused)
{
//To make sure the time is not paused
StartCountdown();
}
}
function StartCountdown()
{
timeRemain = startingTime - Time.time;
if(timeRemain < 0)
{
timeRemain = 0;
isPaused = true;
TimeUp();
}
ShowTime();
}
function ClockPause()
{
isPaused = true;
}
function ClockUnpause()
{
isPaused = false;
}
function ShowTime()
{
var minutes : int;
var seconds : int;
var timeStr : String;
minutes = timeRemain / 60;
seconds = timeRemain % 60;
timeStr = minutes.ToString() + ":";
timeStr += seconds.ToString("D2");
guiText.text = timeStr; //shows the time in the GUI
}
function TimeUp()
{
}
Answer by Griffo · Nov 30, 2012 at 06:33 PM
Just change your line
timeRemain = startingTime - Time.time;
To
timeRemain = startingTime - Time.timeSinceLevelLoad;
Glad I could help, can you mark it as answered please, thanks.
Answer by BiG · Nov 30, 2012 at 04:28 PM
Maybe your script is assigned to an object that exists in all the levels (so, also in Main Menu and Player Select), even if you have no need for that in those levels.
However, supposing that you have the following index structure:
LEVEL NAME LEVEL INDEX
Main Menu 0
Player Select 1
Level 1 2
...
Level n n+1
, you could modify the Update() as follows:
function Update ()
{
if ((!isPaused) && Application.loadedLevel > 1)
{
//To make sure the time is not paused
StartCountdown();
}
}
That is: if the game isn't paused AND you have passed the Main Menu and Player Select screens (so, you are at least in Level 1), launch StartCountDown().
I went in and checked to make sure the other scenes don't have the script and they don't. So I added that code and it's still counting down during the main menu and player select.
Your answer
Follow this Question
Related Questions
Basic toggle timer Help! 2 Answers
Random Delay time 2 Answers
Setting Scroll View Width GUILayout 1 Answer
Best Unity Practices 1 Answer
gameoverscript.js(1,1): BCE0044: expecting EOF, found 'gameOver'. 3 Answers