- Home /
Timer Runtime Problem
I'm using a timer that uses time.time to count down from 60 seconds to 0. The problem is that:
A.) I can't add time, like to extend the count down.
B.) It runs even in the main menu.
Can anyone offer suggestions to remedy this problem?
Here's the code for now:
static var Timer: int = 0; static var startTime: int = 120; var timeLeft: int; var prefix = "Time Left: "; var suffix = " ";
function Awake() { Timer = startTime; }
function Update () {
guiText.text = prefix + timeLeft + suffix;
Timer = Time.time * -1;
timeLeft = startTime + Timer;
if(timeLeft <= 0) { Application.LoadLevel ("Game Over"); }
}
Answer by · Oct 09, 2010 at 02:11 AM
To address the problems you're having, I would manually create a timer, rather than using Time.time.
private var timeRemaining : float = 60.0;
function Update() { timeRemaining -= Time.deltaTime; }
Using this approach:
- you can subtract time in an Update loop you know will only get called when not on the main menu.
- you can add to timeRemaining to extend the countdown.
If you'd rather use Time.time, you could use a few other variables to track bonus time and paused time, but I honestly think this way is simpler. Please ask if you have any questions!
The only problem is that it decreases too fast, not by seconds.
Every Update(), the value will decrease by deltaTime, which is the time for one frame. At 60 frames per second, that's 0.0166 seconds. What's the actual problem that you're having?
Display the timer as an int and it'll round to the nearest integer. Or use mod 1 (%) to subtract the remainder.
No that's still not it. It displays whole numbers, but the rate of decease is too fast.
The timer should decrease at a rate of one number per second, but ins$$anonymous$$d it flies through the numbers, going through 2 $$anonymous$$utes in under 5 seconds.
Answer by AVividLight · Oct 08, 2010 at 06:18 PM
Can you post your script? We can't help unless we can see what you wrote...
This isn't an answer to the question. This should probably be a comment on the question.
Yeah, but I intend to help once I can see his script, so when (or if) he posts the script I plan on editing my post... Although I could have asked him to post his script I don't think it matters... Sorry to confuse you!
-Gibson
Your answer
Follow this Question
Related Questions
Time Counter - up 6 Answers
Dual Timer Problems 1 Answer
countdown/countup timer 1 Answer
counter but with textures? 1 Answer
Lap timer using colliders 1 Answer