I’m making a circular countdown timer, and I already have the sprites and the stuff ready to be used. My problem is just making the timer inside a coroutine (it will be started after a level finishes loading), so I need to count X seconds inside it and display them by reducing the countdown Image fill.
How do I make time count inside a coroutine and get how much time is left?
(using the update function is a no because the class I’m working at is a simple scene manager)
Similar to MacDX’s answer:
void DoStuff () {
// Whatever you want to happen when the countdown finishes
}
IEnumerator Countdown (int seconds) {
int counter = seconds;
while (counter > 0) {
yield return new WaitForSeconds (1);
counter--;
}
DoStuff ();
}
Maybe something like this
public Image countdownImage;
void Start()
{
StartCoroutine(Countdown());
}
private IEnumerator Countdown()
{
float duration = 3f; // 3 seconds you can change this
//to whatever you want
float normalizedTime = 0;
while(normalizedTime <= 1f)
{
countdownImage.fillAmount = normalizedTime;
normalizedTime += Time.deltaTime / duration;
yield return null;
}
}