- Home /
Countdown not counting down
Hi
I'm relatively new to scripting and I made this script so that when 4 players (1 at the moment for testing) enter an area a countdown starts. The problem is that it isn't counting down. Can someone please help me by making it count! Also when it gets to 0 how do I get the player to quit the scene? Thanks.
var playersAtEndPoint = 0;
var guiskin : GUISkin;
var timeTotal = 1800;
var timeUntilEnd = timeTotal / 60;
function OnTriggerEnter (Player : Collider){
playersAtEndPoint ++;
}
function OnTriggerExit (Player : Collider){
playersAtEndPoint --;
}
function Update (){
if (playersAtEndPoint == 1){
timeTotal = timeTotal - Time.deltaTime;
}
if (playersAtEndPoint == 0){
timeTotal = 1800;
}
}
function OnGUI(){
GUI.backgroundColor = Color.black;
GUI.contentColor = Color.white;
GUI.skin = guiskin;
GUI.Label(Rect(1,1,100,100),playersAtEndPoint.ToString());
GUI.Label(Rect(8,8,100,100),"/4");
if (playersAtEndPoint == 1){
GUI.Label(Rect(1,30,100,100),"Time left:" + timeUntilEnd.ToString());
}
}
It's probably just a thing of style, but for
if (playersAtEndPoint == 1){
boolean-variables are common. This way they get declared:
var playersAtEndPoint : boolean = false;
The if-statement is written this way then:
if (playersAtEndPoint){
}
A negative-condition would look like this:
if (!playersAtEndPoint){
}
which is equivalent to:
if (playersAtEndPoint == false){
}
To set the variable's state in the script:
playersAtEndPoint = true;
and
playersAtEndPoint = false;
are used. If you just want to switch the state:
playersAtEndPoint = !playersAtEndPoint;
which will always turn the variable into it's opposite state.
Answer by spacepilot · Dec 28, 2011 at 11:06 PM
Copy the calculation
var timeUntilEnd = timeTotal / 60;
to the end of your script, so that the last lines read:
if (playersAtEndPoint == 1){
timeUntilEnd = timeTotal / 60;
GUI.Label(Rect(1,30,100,100),"Time left:" + timeUntilEnd.ToString());
}
Doing the math outside the function makes it getting calculated only once, before any function is processed.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Cube terrain with perlin noise 1 Answer
Click To Revert to Original Texture else Destroy script help 1 Answer
What is wrong with this script? 2 Answers
How to Implement System.StringBuilder into JavaScript? 2 Answers