Answers for "unity timer"

C#
23

unity timer

float timeLeft = 30.0f;
     
     void Update()
     {
         timeLeft -= Time.deltaTime;
         if(timeLeft < 0)
         {
             GameOver();
         }
     }
Posted by: Guest on February-12-2020
0

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--;
        }
    }
}
Posted by: Guest on March-11-2020
0

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());
    }
Posted by: Guest on July-13-2021
0

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;
}
}
Posted by: Guest on June-15-2021

C# Answers by Framework

Browse Popular Code Answers by Language