• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by marouane01 · May 24, 2016 at 05:02 AM · timer countdown

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







Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image saud_ahmed020 · May 24, 2016 at 07:25 AM 1
Share

@marouane01 Your code is unreadable. $$anonymous$$indly post in readable form.

avatar image marouane01 saud_ahmed020 · May 24, 2016 at 07:52 AM 0
Share

@saud_ahmed Done

avatar image saud_ahmed020 saud_ahmed020 · May 24, 2016 at 08:08 AM 0
Share

@marouane01

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;
         }
     }
 }
 
 
avatar image marouane01 · May 26, 2016 at 12:55 AM 0
Share

@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#

avatar image Mmmpies · May 26, 2016 at 08:37 AM 0
Share

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!

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

56 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Text UI shakes when being updated with a countdown function 1 Answer

timer level i need help . Im new to unity 1 Answer

Need help with a timer in a android 2d game 0 Answers

Timer is not counting down. Staying at original number. 1 Answer

How do I decrease a slider over time? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges