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 /
  • Help Room /
This question was closed Apr 17, 2016 at 05:03 PM by Le-Pampelmuse for the following reason:

Duplicate. Do some research before asking a new question.

http://answers.unity3d.com/questions/348742/time-formatting.html

avatar image
0
Question by FlameCrafter · Apr 17, 2016 at 12:14 PM · countdown

How do you hide milliseconds when making a countdown timer?

I'm making a countdown timer with a M:SS layout. Though when I do it, it shows the milliseconds. Here's what I've done:

 public Text timerText;

 float seconds = 30;
 float minutes = 1;

 void Update ()
 {
     seconds -= Time.deltaTime;

     if (seconds <= 0)
     {
         minutes -= 1;
     }

 }

 void OnGUI()
 {
     timerText.text = minutes + ":" + seconds;
 }

}

I would also like the answer in C Sharp form since I find Javascript hard... very hard..

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 Le-Pampelmuse · Apr 17, 2016 at 05:03 PM 1
Share

What have you tried?

Check out Xarbrough's answer.

You can google most of your questions. This avoids duplicate questions.

3 Replies

  • Sort: 
avatar image
0

Answer by Kurtisi · Apr 17, 2016 at 02:48 PM

Just round up you second counter in the OnGUI method.

 void OnGUI()
      {
          timerText.text = minutes + ":" + Mathf.Round(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 himagolden · Apr 17, 2016 at 03:36 PM

Just change float seconds to

 int seconds=30;


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 Xarbrough · Apr 17, 2016 at 03:16 PM

Take a look at C# string formatting: Custom Numeric Format Strings

Rounding is not actually what you want, for a stopwatch you usually only cut off the smallest increments. String formatting is also very flexible and easy to understand:

     public Text timerLabel;
 
     float seconds = 30;
     float minutes = 1;
 
     void Update()
     {
         seconds -= Time.deltaTime;
         if (seconds <= 0)
         {
             minutes -= 1;
         }
     }
 
     void OnGUI()
     {
         //timerLabel.text = minutes + ":" + seconds;
         timerLabel.text = string.Format("{0:00}:{1:00}", minutes, seconds);
     }

You simple put variables into a string like this:

 string.Format("First variable: {0}, Second variable: {1}", minutes, seconds);

With ":00" you tell the formatter to only use two characters after the period, if the variable is a number.

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

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Unity3d countdown timer is not counting down 0 Answers

Countdown timer for starting game 0 Answers

Need to create a Time Trial in Unity with C# 1 Answer

how to save daily reward timer by playerprefs ? 0 Answers

Hi there i´m new to unity. i have built a labyrinth with timer. how can i set the timer backwards from 15sec. counting down and stop the game at 0? 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