Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by EytanTKing · Jun 26, 2011 at 03:27 PM · textupdatetextmeshrealtimeclock

Realtime clock

How do I make a real time clock in my game so that the player can see it? Would I do it like so:

 var dt = Date();
 private var textMesh : TextMesh;
 var day = dt.Now.Day.ToString();
 var month = dt.Now.Month.ToString();
 var year = dt.Now.Year.ToString();
 var hours = dt.Now.Hour.ToString();
 var minutes = dt.Now.Minute.ToString();
 if (parseInt(minutes) < 10) minutes = "0" + minutes;
 var seconds = dt.Now.Second.ToString();
 if(parseInt(seconds) < 10) seconds = "0" + seconds;
 
 function Start () {
     textMesh = GameObject.Find ("Timer").GetComponent(GUIText);
     textMesh.text = hours.ToString(); 
     textMesh.text = minutes.ToString();
     textMesh.text = seconds.ToString();
 }


And how do I have it the the time (and date) gets updated?

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Bunny83 · Jun 27, 2011 at 12:27 AM

It's way simpler ;). btw you assign hours, minutes and seconds to the same textmesh so you override the others and only the seconds will show up.

Like Ashkan said a coroutine would be the best since the time needs only an updated per sec. It's better to update at least 2 times a sec. to avoid synchronisation interferences.

 function Updatetime()
 {
     while(true)
     {
         var today = System.DateTime.Now;
         textMesh.text = today.ToString("yyyy-MM-dd_HH:mm:ss");
         yield WaitForSeconds(0.2f); 
     }
 }
 
 function Start()
 {
     Updatetime();
 }

I don't know what format you want but you can change the format string as you like ;) Take a look at the this page.

Comment
Add comment · Show 7 · Share
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 Waz · Jun 27, 2011 at 12:41 AM 0
Share

This is the more efficient answer. Always be careful not to do things every frame when a much less frequent update will suffice.

avatar image Waz · Jun 27, 2011 at 12:43 AM 0
Share

If the clock only needed to update every $$anonymous$$ute, the WaitForSeconds time could be set to just a little over the time remaining until the next munite ticks over, for example.

avatar image Ashkan_gc · Jun 27, 2011 at 01:26 AM 0
Share

i told to do this in my answer too. the problem of this ocde is that the variable can be defined outside the function to prevent deleting and creating it once per call.

avatar image Waz · Jun 27, 2011 at 03:13 AM 0
Share

Yes, but it first says to do something expensive in the Update() function. Feel free to edit your answer for more clarity, it's a collaboration, not a competition.

avatar image Ashkan_gc · Jun 27, 2011 at 03:32 AM 0
Share

surely man! i just wanted to mention and nothing more. i think having both there is good because still many novices are afraid of coroutines and don't know how to use them.

Show more comments
avatar image
1

Answer by Ashkan_gc · Jun 26, 2011 at 05:36 PM

just update the text of textMeshes or any other text element that you want to show your date/time in in Update use something like this in your Update

 function Update ()
 {
 textMesh.text = td.Now.Minutes.ToString();
 }

i did not checked for minutes less than 0 but you can and should do it to display 09 instead of 9 as you did yourself in declarations.

now you are using only a textmesh and rewriting it's value 3 times show it will show the seconds. use dt.Time.ToString() to get the complete time in Update or use += for minutes and seconds to concatinate strings instead of replacing them. keep in mind that these string processings are time and memory consumming so doing it in a coroutine instead of Update (for example 5 times a second) can help much. after all the time will not change 70 times a second. so the same hh:mm:ss will be computed and displayed.

 function UpdateTime()
 {
 while(true)
 {
 //calculate times and update strings
 yield WaitForSeconds(0.2f); //5 times a second
 }
 }


and start this UpdateTime in Start/Awake.

Comment
Add comment · Share
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
0

Answer by jimrota · Feb 21, 2016 at 10:41 PM

I ended up doing this for the new UI system (have to add using UnityEngine.UI namespace)

public Text textDate; //had to attach a UIText component to this

//then in some script

 IEnumerator UpdateTime()  {
     while(true)
     {
         var today = System.DateTime.Now;
         textDate.text = today.ToString("yyyy:MM:dd @ HH:mm:ss");
         yield return new WaitForSeconds(0.2f);
     }
 }

And then call StartCoroutine(UpdateTime()) from the Start()

Not sure if I need the while(true) clause, but it works.

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Why if I execute .SetText on the Start it dont works. 1 Answer

Display Emojis using its HTML code in a text 1 Answer

TMP text dosent show up in android. 0 Answers

TextMeshPro change value with slider 1 Answer

Unity crashes at writing TextMesh? 0 Answers


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