So I’m trying to get a basic countdown working. I had some problems, so I stripped it down and started debugging. I ended up taking the code that decreased the value incrementally out, and it revealed I was having larger issues.
var countdown = 5;
function Update () {
if(Input.GetButton("Fire1")){
var countdown = 5;
}
var countdownClock = countdown; // I took the decreasing value out of here for testing purposes
print ("CountdownClock: " + countdownClock);
}
The print statement comes up with 0 every frame. If I left click (“Fire1”), it sets it to 5 for a frame like it should, but then immediately goes back to 0. It’s like something somewhere is telling to constantly be 0. I’ve searched all my code for “countdown” and it’s not in any other scripts. Ideas?
my new problem is that it’s incrementing down 1 every frame, instead of every second. I’ve used Time.deltaTime before to fix this, but I must be doing it wrong here.
Thanks for the snarky response, but I use the unity script reference all the time. I’m using it exactly as described, which is why I’m confused. In fact, I’ve tried creating a variable that houses the “1 * Time.deltaTime” equation, just like it does in the reference example, but that doesn’t get me any results. If you’d even looked at the code, you’d have seen that
That didn’t work nulldiver, I had already tried that.
I scrapped all my code and started over with a new approach. Now I have a new issue (oh joy), relating to WaitForSeconds.
What happens here is that on Awake, it does the countdown perfectly. I see 5 through 0 print out every second. But when I activate “Fire1”, it prints “5” every frame for a second, then 4 through 0 every frame and then stops. Why is it working on Awake, but not when I call the function elsewhere?
SOLVED! Took me way too long, but I got it. Here’s the working code for anyone who may read this in the future.
var currentTime = 5;
private var counting :boolean;
function Countdown(){
counting = true;
for(var i = currentTime; currentTime >= 0; currentTime-- ){
print ("Current Time: " + currentTime);
yield WaitForSeconds(1);
}
counting = false;
}
function Awake(){
Countdown();
}
function Update () {
if(Input.GetButton("Fire1")){
currentTime = 5;
if(!counting){
Countdown();
}
}
Added the var “counting”, and then only allowed it to execute Countdown if it was false. Also made sure that if the player clicked and set currentTime to 5, the for loop was using the var currentTime, and not i.
For anyone who may read this in the future – what was posted earlier in this thread would go from 5 to 0 in 5 frames instead of 5 seconds (which was an actual scripting problem), but with currentTime explicitly declared as a float, it takes 5 seconds, as you would expect. In most cases, this is going to work just fine and there is no reason to use a coroutine and yield WaitForSeconds(1) for a simple countdown. Without the coroutine, it would be ideal for showing a timer in GUI (just Mathf.Floor(currentTime) if you only wanted to show the integer value counting down, changing every second). It would also be a perfectly fine way to evaluate time remaining in your game logic (ie. speed up movement, music, etc. as the time counts down).