countdown script in unity
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--;
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
using UnityEngine.UI;
public class TimerCountDown : MonoBehaviour
{
public GameObject textDisplay;
public int SecondsLeft = 50;
public bool takingAway = false;
void Start()
{
textDisplay.GetComponent<Text>().text = "00:" + SecondsLeft;
}
void Update()
{
if (takingAway == false && SecondsLeft > 0)
{
StartCoroutine(TimerTake());
}
}
IEnumerator TimerTake()
{
takingAway = true;
yield return new WaitForSeconds(1);
SecondsLeft -= 1;
if(SecondsLeft < 10)
{
textDisplay.GetComponent<Text>().text = "00:0" + SecondsLeft;
}
else
{
textDisplay.GetComponent<Text>().text = "00:" + SecondsLeft;
}
textDisplay.GetComponent<Text>().text = "00:" + SecondsLeft;
takingAway = false;
}
// Start is called before the first frame update
void FixedStart()
{
}
// Update is called once per frame
void FixedUpdate()
{
}
}