Count down timer C#
I feel like there is something I'm missing but I don't know why I can't get my text in my canvas to display the time left float value. how do I fix this
CountDown.cs:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class CountDown : MonoBehaviour
{
float timeLeft = 300.0f;
Text text;
void Awake ()
{
text = GetComponent<text> ();
}
void Update()
{
timeLeft -= Time.deltaTime;
text.text = "time Left:" + timeLeft;
if(timeLeft < 0)
{
Application.LoadLevel("gameOver");
}
}
}
Answer by Nomabond · Jun 04, 2015 at 03:02 PM
Hi Nocturnal,
On line 13 you are using GetComponent(); but the actual component you are trying to find is a Text (notice the capital 'T' ).
It would be much easier for you though if you set your Text text to a public variable and then use the inspector to drag the reference of your UI Text into the script.
Here is a slightly modified version of your script that you can use to get a nice rounded integer countdown from 300. Let me know if you have any questions about it!
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class CountDown : MonoBehaviour
{
float timeLeft = 300.0f;
public Text text;
void Update()
{
timeLeft -= Time.deltaTime;
text.text = "Time Left:" + Mathf.Round(timeLeft);
if(timeLeft < 0)
{
Application.LoadLevel("gameOver");
}
}
}
Thanks this worked for me! i changed a little but hey.
Hey I want to makr text box in which user will put value and after that value sound must play. How to do that plz help. I am new in this field
Answer by deLord · Oct 03, 2015 at 10:57 AM
I found Nocturnals timer a bit crude, so I came up with this:
using System.Collections;
using UnityEngine.UI;
public class Countdown : MonoBehaviour {
public float timeLeft = 300.0f;
public bool stop = true;
private float minutes;
private float seconds;
public Text text;
public void startTimer(float from){
stop = false;
timeLeft = from;
Update();
StartCoroutine(updateCoroutine());
}
void Update() {
if(stop) return;
timeLeft -= Time.deltaTime;
minutes = Mathf.Floor(timeLeft / 60);
seconds = timeLeft % 60;
if(seconds > 59) seconds = 59;
if(minutes < 0) {
stop = true;
minutes = 0;
seconds = 0;
}
// fraction = (timeLeft * 100) % 100;
}
private IEnumerator updateCoroutine(){
while(!stop){
text.text = string.Format("{0:0}:{1:00}", minutes, seconds);
yield return new WaitForSeconds(0.2f);
}
}
}
Hope this helps as well
yiepppp thank you so much for this its save my time with counter thanks buddy .!!!
How about if I want to decrease it by 10 seconds after I click 4 times :) and the count of the click will back as 0. Then the whole process continue as the timer goes 0. HOpe you answer :)
Here's the code.
void Update() { countdownTime -= Time.deltaTime; if (DisplayItems.totalclicks==4) { decreaseTime += 10;
DisplayItems.totalclicks = 0;
}
$$anonymous$$utes = $$anonymous$$athf.FloorToInt(countdownTime / 60F);
seconds = $$anonymous$$athf.FloorToInt(countdownTime - $$anonymous$$utes * 60) -decreaseTime;
string niceTime = string.Format("{0:0}:{1:00}", $$anonymous$$utes, seconds);
text.text = niceTime;
}
How do you check when it reaches 00:00? This is my code, but it won't work. if(($$anonymous$$utes==0) && (seconds == 0)) { stop = true; timer.gameObject.SetActive(false); }
Answer by MyrDovg · Oct 30, 2017 at 11:31 AM
float totalTime = 120f; //2 minutes
private void Update()
{
totalTime -= Time.deltaTime;
UpdateLevelTimer(totalTime );
}
public void UpdateLevelTimer(float totalSeconds)
{
int minutes = Mathf.FloorToInt(totalSeconds / 60f);
int seconds = Mathf.RoundToInt(totalSeconds % 60f);
string formatedSeconds = seconds.ToString();
if (seconds == 60)
{
seconds = 0;
minutes += 1;
}
timer.text = minutes.ToString("00") + ":" + seconds.ToString("00");
}
Necropost and wrong on many levels :
You can't modulo a float
If you use a modulo correctly your whole
if(seconds == 60)
block is uselessYou don't use your
formatedSeconds
No reference to
timer
with your
$$anonymous$$athf.RoundToInt
your actual totalTime will hit zero 0.5s after your text did, what ?!?
And that's not even the worst part : You've been copy and pasting this clumsy code on multiple questions that where already answered.
Just dont do that, please ...
Answer by creatorOFgravityracer · Dec 09, 2017 at 08:38 AM
GravityGamezProd
HERES THE FIXZ TO ALL WHO IS STILL LOOKING AT THIS COUNTDOWN SCRIPT ON UNITY 5
*TODAYS DATE 12/9/17 YOUR WELCOME
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class CountDown : MonoBehaviour { public float timeLeft = 300.0f;
Text text;
void Awake ()
{
text = GetComponent<Text> ();
}
void Update()
{
timeLeft -= Time.deltaTime;
text.text = "time Left:" + timeLeft;
if(timeLeft < 0)
{
Application.LoadLevel("gameOver");
}
}
}
Answer by U_Ku_Shu · Nov 25, 2016 at 10:09 AM
float _countDownTimeLeft = 30.0f;
bool _timerIsGoing;
void Update()
{
UpdCountDown();
}
private void UpdCountDown()
{
if (_timerIsGoing && _countDownTimeLeft > 0)
{
_countDownTimeLeft -= Time.deltaTime;
}
}