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");
		}
	}
}

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

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");
		}
	}
}

float _countDownTimeLeft = 30.0f;
bool _timerIsGoing;
void Update()
{
UpdCountDown();
}

private void UpdCountDown()
{
    if (_timerIsGoing && _countDownTimeLeft > 0)
    {
        _countDownTimeLeft -= Time.deltaTime;
    }
}

public class Timercontroler : MonoBehaviour {
public Text timer;
float timeleft=300f;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
	timeleft -= Time.deltaTime;
	int minute = Mathf.FloorToInt (timeleft / 60f);
	int second = Mathf.FloorToInt (timeleft -minute*60);
	timer.text = minute.ToString()+":"+second.ToString();
		
}

}

So this is the super basic timer I am currently working on. I am using it for a world space UI so… But hopefully it works for all of you guys.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CountDownTimer : MonoBehaviour {

    public Text timerText;
    [SerializeField] private float timeLimit = 10f;
   
    private bool activeTimer = true;
    private float t;

    GameObject player;//reference to the player gameobject (not sure if needed)
    PlayerHealth playerHealth;// reference to the health script
    private void Awake()
    {
        player = GameObject.FindGameObjectWithTag("Player");
        playerHealth = player.GetComponent<PlayerHealth>();
    }




    // Update is called once per frame
    void Update() {
        if (!activeTimer)
            return;
        t = timeLimit - Time.time;
        string minutes = ((int)t / 60).ToString();
        string seconds = (t % 60).ToString("f2");
        timerText.text = minutes + ":" + seconds;
        if (t <= 0)
        {
            TimeRanOut();
        }

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");
        }

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");
	}
}

}