How to Make a time Clock contdown
Hi there i'm trying to make a contdown real time clock so on this script when i call a function a time variable set as the next tow hours for Ex : if it's 22:51:12 the the variable gonna be 00:51:12 then a UI text show the contdown like this 1:59:15 and when it gonna be 00:00:00 it stop
i have done this script but it dos not work properly
var TH :int;
var TM :int;
var TS :int;
function Update (){
var t =System.DateTime.Now;
if(TS>0 && TM>=0 && TH>=0)
{GifTTime.text = String.Format("{0:D2}:{1:D2}:{2:D2}",TH-t.Hour,TM-t.Minute,TS-t.Second);}
else
{GifTTime.text = "00:00:00";}
}
function SetTime (){
var t :System.DateTime=System.DateTime.Now;
if(t.Hour+1>=24)
{TH= t.Hour+3;TH=TH-24;}else{TH= t.Hour+3;}
TM=t.Minute;
TS=t.Second;
}
@marouane01 Your code is unreadable. $$anonymous$$indly post in readable form.
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class Timer : $$anonymous$$onoBehaviour
{
public delegate void TimeOut();
public static event TimeOut OnTimeOut;
private static Timer _sharedInstance;
private string textTime;
private int $$anonymous$$utes;
private int seconds;
private Text textField;
private float countDownTime;
public static float selectedTime$$anonymous$$ode;
void Awake()
{
_sharedInstance = this;
setCountDownTime();
textField = GetComponent<Text>();
updateTimerLable();
}
void Update()
{
if(GameController.isGamePause)
return;
if (countDownTime < 0)
{
if (OnTimeOut != null)
OnTimeOut();
}
else
{
countDownTime -= Time.deltaTime;
updateTimerLable();
}
}
void OnDestroy()
{
OnTimeOut = null;
}
public static Timer SharedInstance()
{
return _sharedInstance;
}
void updateTimerLable()
{
$$anonymous$$utes = $$anonymous$$athf.FloorToInt(countDownTime / 60F);
seconds = $$anonymous$$athf.FloorToInt(countDownTime % 60);
textTime = string.Format ("{0:00}:{1:00}", $$anonymous$$utes, seconds);
textField.text = textTime;
}
public float CountDownTime
{
get{
return countDownTime;
}
set{
countDownTime = value;
}
}
public float $$anonymous$$inutes
{
get{
return $$anonymous$$utes;
}
}
public float Seconds
{
get{
return seconds;
}
}
}
@saud_ahmed020 actually that help a bit but what i need it to create a function that can be called ones every 2 hours and a UI Text that show the rest of time [Using JavaScript] cos this just a part of the game script cos i'm not good on C#
Well my Javascript isn't great but in theory...
#pragma strict
var countDown : float = 60.0; // set to a $$anonymous$$ute
function Update()
{
countDown -= Time.deltaTime;
if(countDown <= 0)
{ countDown = 0; }
}
Then just display it and change that if statement so it doesn't just stop counting.
EDIT - sorry got your post mixed up with another! You were asking something far more complex!
Your answer
![](https://koobas.hobune.stream/wayback/20220612081424im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
(Help Please) How to load new level one count down time hits zero? 1 Answer
OnApplicationQuit Timer 0 Answers
(CRASH) how do I fix my teleporter code? 2 Answers
How to make a depleting bar timer GUI please help! 0 Answers
Timer in Coroutines not working 0 Answers