maxhacker11

Countdown.cs

Sep 5th, 2023 (edited)
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System;
  6.  
  7. public class Countdown : MonoBehaviour
  8. {
  9.     [SerializeField] private Text timerText;
  10.  
  11.     public float currentTime = 30f;
  12.     private bool active = true;
  13.  
  14.     private void Update()
  15.     {
  16.         if (!active)
  17.             return;
  18.  
  19.         currentTime -= Time.deltaTime;
  20.  
  21.         UpdateTimerUI();
  22.  
  23.         if (currentTime <= 0)
  24.         {
  25.             StopTimer();
  26.         }
  27.     }
  28.  
  29.     public void StopTimer()
  30.     {
  31.         active = false;
  32.         currentTime = 0f;
  33.         UpdateTimerUI();
  34.     }
  35.  
  36.     private void UpdateTimerUI()
  37.     {
  38.         if (currentTime > 0 && currentTime < 6)
  39.         {
  40.             timerText.color = Color.yellow;
  41.         }
  42.         else if (currentTime < 1)
  43.         {
  44.             timerText.color = Color.red;
  45.         }
  46.  
  47.         TimeSpan t = TimeSpan.FromSeconds(currentTime);
  48.         timerText.text = t.ToString(@"mm\:ss");
  49.     }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment