Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using System;
- public class Countdown : MonoBehaviour
- {
- [SerializeField] private Text timerText;
- public float currentTime = 30f;
- private bool active = true;
- private void Update()
- {
- if (!active)
- return;
- currentTime -= Time.deltaTime;
- UpdateTimerUI();
- if (currentTime <= 0)
- {
- StopTimer();
- }
- }
- public void StopTimer()
- {
- active = false;
- currentTime = 0f;
- UpdateTimerUI();
- }
- private void UpdateTimerUI()
- {
- if (currentTime > 0 && currentTime < 6)
- {
- timerText.color = Color.yellow;
- }
- else if (currentTime < 1)
- {
- timerText.color = Color.red;
- }
- TimeSpan t = TimeSpan.FromSeconds(currentTime);
- timerText.text = t.ToString(@"mm\:ss");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment