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 Chessnutter · Dec 25, 2017 at 08:43 AM · beginnersyntax

How can I make a timer follow the format 00:00 (Minutes, Seconds)

Hello!

I am making a timer for my game, and I want it to follow the format of 00:00.

The code I am using means that 5m 4s, or similar, comes up as 5:4, and I want it to come up as 05:04. If anyone knows how to fix this please let me know! Thanks!


 void Update () {
     float t = Time.time - StartTime;
     string minutes = ((int)t / 60).ToString();
     string seconds = (t % 60).ToString("f0");

     TimerText.text = minutes + ":" + seconds;
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by theterrificjd · Dec 25, 2017 at 08:51 AM

 Timer.text = string.Format("{0:0}:{1:00}s",minutes,seconds);

should do it I think.

{0:0} the first 0, before the :, is the first argument you pass after the string (ie: minutes). The second 0, after the :, is a formatting tool saying this can only be one integer into a string here. The first integer being the only integer you want, this works.

{1:00} is similar, but gets the second argument and gets the first two integer chars.

You can also pass your arguments as an array if you have many.

 void Update () {
      float t = Time.time - StartTime;
      float minutes = (t / 60);
      float seconds = (t % 60);
      Timer.text = string.Format("{0:00}:{1:00}",minutes,seconds);
  }
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 Chessnutter · Dec 25, 2017 at 08:56 AM

Sorry, but it doesn't work. It just adds milliseconds and an 'S' on the end, neither of which I want :( Thanks for trying though

Comment
Add comment · Show 5 · 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 theterrificjd · Dec 25, 2017 at 09:01 AM 0
Share

For this method, your $$anonymous$$utes and seconds can't be strings, by the way, they'd need to be changed to floats (just change the declaration and remove the ToString, as the string.Format is a more reliable way to turn floats or integers into strings.

avatar image theterrificjd · Dec 25, 2017 at 09:09 AM 0
Share

See if my updated response helps.

avatar image Chessnutter · Dec 25, 2017 at 09:10 AM 0
Share

Ok, that works reasonably well, except when the timer hits 60 seconds it actually displays 0:60, then goes to 1:01. Is there a fix for this? Thanks!

avatar image theterrificjd Chessnutter · Dec 25, 2017 at 11:35 AM 0
Share

Sorry for late reply. It is because it is a float, and it's rounding upwards I believe. What you want is to round it down.

 seconds = $$anonymous$$athf.Floor(t % 60);

$$anonymous$$athf.Floor will round down to the nearest integer, where as $$anonymous$$athf.Ceil will round up. This should solve it, if not, you may need to sloppily add a -1 somewhere.

avatar image theterrificjd · Dec 25, 2017 at 09:12 AM 0
Share

https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings

It's also possible that {0:D2} may be what you want. This list is definitely worth bookmarking though, as string.Format is very useful.

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

77 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 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

Greater than and less than syntax problem 1 Answer

the type or namespace name Note could not be found. are you missing a using directive or an assembly reference. 0 Answers

Basic javascript array declaration 2 Answers

Syntax for a game object component variable on a transform in an FBX hierarchy? 1 Answer

Developing for iOs (is there a certain syntax?) 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