Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by TurboHermit · May 29, 2012 at 08:19 PM · timetimercountdownminutes

Format String Minutes:Seconds

Hi I'm making a timer that counts down a variable. I'm trying to format the string in my GUI. The variable is time in seconds.

Is it possible to keep this one variable or do I need to split the variables into minutes and seconds? And what's the line for formatting time?

Comment
Add comment · Show 1
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 Scribe · May 29, 2012 at 08:28 PM 0
Share

keeping just one variable you could probably have a line like


print((TimeVariable % 60) + ":" + (TimeVariable - (TimeVariable % 60) * 60));
though it could be more efficient and easier to do it a different way or with two variables, I am unsure.

3 Replies

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

Answer by whydoidoit · May 29, 2012 at 08:30 PM

Try this for size:

 var someString = String.Format("{0:0}:{1:00}", Mathf.Floor(yourTime/60), yourTime % 60);
Comment
Add comment · Show 2 · 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 TurboHermit · May 29, 2012 at 09:14 PM 0
Share

Perfect, but apparently the $$anonymous$$ute number stays even tough the $$anonymous$$ute is over. Like if I have 120 seconds the timer states 2:00 then after two seconds 2:58. Is there any way to avoid this?

avatar image whydoidoit · May 29, 2012 at 11:15 PM 0
Share

Sorry yes of course I guess it is rounding it up!

try:

  var someString = String.Format("{0:0}:{1:00}", $$anonymous$$athf.Floor(yourTime/60), yourTime % 60);
avatar image
1

Answer by flamy · May 29, 2012 at 08:43 PM

This is just a elaborate version of @mike's solution

 #pragma strict
 
 var time:int=0;
 function Start () {
 
 time=100;
 InvokeRepeating("countDown",1f,1f);
 }
 
 function Update () {
 
 
 
 }
 function countDown()
 {
     time-=1;
 }
 
 function  OnGUI(){
     var minute:int =  (int) Mathf.Abs(time/60);
 
     var seconds:int =time%60;
     var labelString: String=minute.ToString("00")+":"+seconds.ToString("00");
     GUILayout.Label(labelString);
 }
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 Threezool · Oct 26, 2015 at 12:30 PM

Made my own version to display a "time elapsed" counter in Unity 5 and display it in a canvas:

 DateTime startTime;
 TimeSpan elapsedTime;
     
 void Start () {
     startTime = DateTime.Now;
 }
         
 void LateUpdate () {
     elapsedTime = DateTime.Now - startTime;
     
     string displayTime = String.Format("{0:00}:{1:00}:{2:000}", 
     elapsedTime.Minutes, 
     elapsedTime.Seconds, 
     elapsedTime.Milliseconds);
     
    GetComponent<Text>().text = displayTime;
 }

Dont forget to add "using System;" to be able to use DateTime.

Comment
Add comment · Show 1 · 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 hzli · Feb 23, 2019 at 04:15 AM 0
Share

I know this is an old post. How can I count backward?

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

9 People are following this question.

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

Related Questions

Coroutine Countdown Timer being extremely slow 1 Answer

Problem with countdown timer. 3 Answers

how to make a varied countdown timer 1 Answer

Timer doesn't work properly 2 Answers

Time does not start counting down when need to 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