• 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
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 t$$anonymous$$s one variable or do I need to split the variables into minutes and seconds? And what's the line for formatting time?

Comment
rocket5tim
Spielio

People who like this

2 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
Best Answer

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

Try t$$anonymous$$s for size:

 var someString = String.Format("{0:0}:{1:00}", Mathf.Floor(yourTime/60), yourTime % 60);
Comment
flamy
You!
rocket5tim

People who like this

3 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 minute number stays even tough the minute 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}", Mathf.Floor(yourTime/60), yourTime % 60);
avatar image

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

T$$anonymous$$s 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
You!

People who like this

1 Show 0 · 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

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

People who like this

0 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.

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

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

Countdown Timer Help About putting 0's 1 Answer

How to stop a Countdown Timer? 1 Answer

Problem with countdown timer. 3 Answers

how to make a varied countdown timer 1 Answer

Timer doesn't work properly 2 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