unity timer
float timeLeft = 30.0f;
void Update()
{
timeLeft -= Time.deltaTime;
if(timeLeft < 0)
{
GameOver();
}
}
unity timer
float timeLeft = 30.0f;
void Update()
{
timeLeft -= Time.deltaTime;
if(timeLeft < 0)
{
GameOver();
}
}
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--;
}
}
}
unity timer
/*IEnumerator approach*/
/*Destroy object after 5 seconds*//**//**//**//**//**//**//**//**//**/
//start coroutine on start
private void Start()
{
StartCoroutine(destroy());
}
//create destroy coroutine
IEnumerator destroy()
{
//wait 5 seconds
yield return new WaitForSeconds(5);
//destroy this gameobject
Destroy(this.gameObject);
}
/*Make seconds counter*//**//**//**//**//**//**//**//**//**//**//**//**/
private float time = 0;
private Text displayText;
private void Start()
{
StartCoroutine(mainTick());
}
//create mainTick coroutine
IEnumerator mainTick()
{
//wait 1 second
yield return new WaitForSeconds(1);
//add 1 second to the time variable
time++;
//update UI
displayText.text = time.ToString();
//start mainTick again
StartCoroutine(mainTick());
}
unity timer
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Timerexample : MonoBehaviour
{
float val;
bool srt,stp,rst;
public Text disvar;
void Start()
{
val=0;
srt=false;
stp=false;
rst=false;
}
void Update()
{
if(srt)
{
val += Time.deltaTime;
}
double b = System.Math.Round (val, 2);
disvar.text = b.ToString ();
}
public void stopbutton()
{
srt=false;
}
public void resetbutton()
{
srt=false;
val=0;
}
public void startbutton()
{
start=true;
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us