You've successfully subscribed to Hacking with Unity - Learn how to create your own games
Great! Next, complete checkout for full access to Hacking with Unity - Learn how to create your own games
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

Unity: Countdown Timer

Ben Liu
Ben Liu

Use Coroutine to create a count down timer:

private float currCountdownValue = 0;

public IEnumerator StartCountdown(float countdownValue = 10) {
    currCountdownValue = countdownValue;
    while (currCountdownValue > 0) {
        Debug.Log("Countdown: " + currCountdownValue);
        yield return new WaitForSeconds(1.0f);
        currCountdownValue--;
    }
}

// to use
StartCoroutine(StartCountdown());
// or 
StartCoroutine(StartCountdown(10));

Ben Liu

Please feel free to contact me if you have any question. <br> https://t.me/NeedoneApp