- Home /
How do i reset a timer once it hits 0?
Im trying to make a timer in my game. What i want it to do is when i click on the icon, I want it to count down. But when it hits 0, i want the timer to reset to the original number and wait for the next click on the timer. I am a huge noob when it comes to coding so any help?
var timer : float = 5;
var isClicked = false;
function OnMouseUp(){
if(timer > 0){
isClicked = true;
}
}
function Update(){
if (isClicked == true){
if (timer > 0){
timer -= Time.deltaTime;
}
if (timer == 0){
timer = 5;
}
}
}
function OnGUI () {
GUI.Box (new Rect (300, 60, 50, 20), "" + timer.ToString("0"));
}
Answer by Jeff-Kesselman · Jun 08, 2014 at 04:13 PM
First of all you have a logic problem here:
if (timer > 0){
timer -= Time.deltaTime;
}
if (timer == 0){
timer = 5;
}
What happens if Time.deltaTime is greater then what is left in timer?
Think about it...
Ok i did fix that after thinking about it. But now that i have that down i just want it to reset to the original number (which it does)and stop (which is doesnt) and wait to be clicked again. Any other way to do that?
Answer by Nonnicus · Jun 08, 2014 at 07:42 PM
Thank you for your answer! i finally figured a script out that works from that small advise that you helped me with. Heres the new script
var timer : float;
var isClicked = false;
var sound : AudioClip;
function OnMouseUp(){
isClicked = true;
}
function Update(){
if (isClicked == true){
if (timer > 0){
timer -= Time.deltaTime;
}
else if(timer <= 0){
timerReset();
audio.clip = sound;
audio.Play();
}
}
}
function OnGUI () {
GUI.Box (new Rect (300, 60, 50, 20), "" + timer.ToString("0"));
}
function timerReset(){
isClicked = false;
if (isClicked == false){
timer = 5;
}
}
Your answer
Follow this Question
Related Questions
Time limit on my game with selection menu 0 Answers
Reset the countdown timer 2 Answers
How To Reset Timer on a Collision (C#) 0 Answers
Timer reset after elapsed time. 1 Answer
Resetting Combos 1 Answer