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"));
}
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…
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;
}
}