timer countdown unity code example

Example 1: unity timer

float timeLeft = 30.0f;
     
     void Update()
     {
         timeLeft -= Time.deltaTime;
         if(timeLeft < 0)
         {
             GameOver();
         }
     }

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;
        }
        
    }