unity count down timer code example

Example 1: unity countdown script

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class  Timer : MonoBehaviour
{
    public int timeLeft = 20;
    public Text countdownText;
    GameObject timeuptext;
    GameObject timeend;
    public int timeCountdownPlays;
    public AudioClip CountDownSound;
    public AudioSource SoundSource;
    

    
    void Start()
    {
        timeuptext = GameObject.Find("TimeUp");
        timeuptext.SetActive(false);
        timeend = GameObject.Find("Timer Text");
        StartCoroutine("LoseTime");
        SoundSource.clip = CountDownSound;
    }

  
    void Update()
    {
       
        countdownText.text = ("" + timeLeft);
            

        if (timeLeft <= 0)
        {
            timeuptext.SetActive(true);
            timeend.GetComponent<Text>().enabled = false;
            
        }

        if (timeLeft == timeCountdownPlays)
        {
            SoundSource.Play();
        }
            
            
        
      
       
    }

    IEnumerator LoseTime()
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            timeLeft--;
        }
    }
}

Example 2: unity countdown clock

//This uses digital clock format (3:45) ««

	using TMPro; //IF YOUR USING TEXT MESH PRO

    TextMeshProUGUI timerLabel;

    float timeLeft = 300; //5 minitues

    private void Update()
    {
        if (timeLeft > 0)
        {
            timeLeft -= Time.deltaTime * 2;

            string minituesLeft = Mathf.FloorToInt(timeLeft / 60).ToString();
            string seconds = (timeLeft % 60).ToString("F0");
            seconds = seconds.Length == 1 ? seconds = "0" + seconds : seconds;

            timerText.text = minituesLeft + ":" + seconds;
        }
        
    }