unity timer
float timeLeft = 30.0f;
void Update()
{
timeLeft -= Time.deltaTime;
if(timeLeft < 0)
{
GameOver();
}
}
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--;
}
}
}
Also in C#:
- c# send email
- how to get object to spawn in a curcle
- play animation through script unity
- how to compare datetime in c#
- sum the digits in c#
- c# stop loop
- use enter key unity
- c# property get set
- c# polymorphism
- c# add to array
- c# code snippet template
- project tab unity
- c# how to refresh your binding source
- bool toggle unity c#
- how to check if textbox is empty in c#
- c# datatable copy selected rows to another table
- unity collider2d contains point
- split string
- what is the and in c#
- round vector3 unity
- c# unity rotate first person controller script
- HashSet C# append
- c# new dictionary linq
- dontdestroyonload unity