Getting a timer to go to 0
Hey everyone,
What i'm trying to do is after the timer goes to 0 i'd like for the timer to stop for 2 seconds and then reset and thus the process will repeat. What i have so far is...
public GUIText timerText;
public float timer = 0.0f;
public float timerMax;
void Start()
{
timer = timerMax ;
}
void Update()
{
if (timer <= 0.0f)
{
StartCoroutine(wait(2.0f));
timer = timerMax;
}
else
{
countdown();
}
}
void countdown()
{
timer -= Time.deltaTime;
updateTimerText();
}
IEnumerator wait(float waitTimer)
{
yield return new WaitForSeconds(waitTimer);
}
void updateTimerText()
{
timerText.text = "Timer: \n" + Mathf.Round(timer * 10f) / 10f;
}
This is also used with a GUIText to print out the timer but that shouldn't really matter. The issue at the moment is that it's not stopping for the 2 seconds. I think this might be due to it not reaching 0 but then again the timer does reset so i'm not too certain.
Any help will be appreciated!
in essence, you need your Coroutine to repeat itself, because it's already waiting. you could wrap the yield in a while true and call whatever you need to be called after the 2 seconds after the yield. Start the coroutine in start.
@hexagonius, awesome you were right. Thanks for the help!
Answer by Tycellent · Sep 05, 2015 at 11:33 AM
If anyone is interested, just as @hexagonius said i just had to use a while loop :)