Unity 5 - Adding a Pre-made Countdown Timer C# Script to the Game Scene as a Text UI Element

Hello I am currently Devolping a Game Scene and require a countdown timer to appear in the top left corner of the screen as a text UI element. I already have a script prepared and simply need to add the component to the game scene.

I have created a text object (called CountdownTimer) in the canvas section of my hierarchy. Created a blank game object, added the script as a component and have added the relevent text object where asked for

Alas this has not worked and I’m unsure why but I believe it to be the way the text object is set up or the way in which I have called upon my script

Here’s the relevent C# code:

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

public class CountdownTimer : MonoBehaviour {

public int maxCountDownTimeLeftInSecond = 15;
public Text Text_Countdown_Timer = null;

public static double secondsLeft = 0.0;

DateTime GameStartDateTime;

void Start ()
{
    GameStartDateTime = DateTime.Now;
}

void FixedUpdate ()
{
    TimeSpan currentTimeLeft = DateTime.Now - GameStartDateTime;
    secondsLeft = maxCountDownTimeLeftInSecond -         
    currentTimeLeft.TotalSeconds;

    if (secondsLeft > 0) 
    {
        Text_Countdown_Timer.text = "Time Left: " + string.Format (" 
        {0:0.00s", secondsLeft);
    } else {
        Text_Countdown_Timer.text = "Time Left: 0.00s";
    }
}
}

I appologise if this is a simple question and I’m missing something very obvious but any help is much appriciated. Thank you

I believe you can do this easily using Time.deltatime. I made a little scene to illustrate this. Consider the images

and the little code below.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CountDownTimer : MonoBehaviour {
    //Reference to the text object
    public Text TimeText;
    private float minutes;
    public GameObject PanelObj;
	// Use this for initialization
	void Start () {
        PanelObj.SetActive(false);
	}
	
	// Update is called once per frame
	void FixedUpdate () {
        CountDown(TimeText,50);
	}
    void CountDown(Text TimeObj, float Totaltime) {
        minutes += Time.deltaTime;
        float seconds = minutes % 60;
        //Reduce the total time by the amount of seconds elapsed...
        Totaltime -= seconds;
        TimeObj.text = "Time Elapsed: " + seconds.ToString("0")+" seconds"+ " Time Remaining: "+ (int)Totaltime;
        if (Totaltime <= 40f) {
            PanelObj.SetActive(true);
            TimeText.enabled = false;
        }
    }
}