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 /
avatar image
1
Question by Vuzok · Jul 01, 2016 at 03:11 PM · timetimertime.deltatimedeltatimetimer-script

Milliseconds Timer Question

Am I getting the milliseconds correct at the moment and how do I make it so that they stop at 1000 and don't just keep going? Also I will want to make them appear as just two digits but I think theres already a question covering that. Any help would be much appreciated!

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Timer : MonoBehaviour {
 
     public Text counterText;
 
     public float milliseconds, seconds, minutes, hours;
 
     void Start () {
         
         counterText = GetComponent<Text> () as Text;
     }
 
     void Update () {
         hours = (int)(Time.timeSinceLevelLoad / 3600f);
         minutes = (int)(Time.timeSinceLevelLoad / 60f);
         seconds = (int)(Time.timeSinceLevelLoad % 60f);
         milliseconds = (int)(Time.timeSinceLevelLoad * 6f);
 
         counterText.text = hours.ToString ("00") + ":" + minutes.ToString ("00") + ":" + seconds.ToString ("00") + ":" + milliseconds.ToString("00"); 
     
     }
 }
 
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

1 Reply

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

Answer by Eno-Khaon · Jul 01, 2016 at 06:22 PM

The basic unit of Time in Unity is 1.0 per second. It's why your implementation of seconds is correct as-is.

However, all your other units are off slightly. For example, 1 hour, 20 minutes, 36 seconds would display as something like 1:80:36 at present.

To account for this, ensure that your minutes are rolled over in the same manner as seconds are:

 minutes = (int)(Time.timeSinceLevelLoad / 60f) % 60;

As for milliseconds, it's a simple application of the metric prefix milli-. Because 1.0 in time = 1 second, you can multiply that by 1000 for an integer value of milliseconds.

 milliseconds = (int)(Time.timeSinceLevelLoad * 1000f) % 1000;

Don't forget to reset the value every second.

Finally, we get to the text formatting. Based on your example, I'm guessing you intend to always display two digits per number displayed, so I'll work on that assumption...

Microsoft keeps some handy documentation on Standard Numeric Format Strings which gives us some useful tools here. Namely, because all your values are converted into integers, you can make use of the "Decimal" listing.

 counterText.text = hours.ToString ("D2") + ":" + minutes.ToString ("D2") + ":" + seconds.ToString ("D2") + ":" + milliseconds.ToString("D2");
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 Vuzok · Jul 02, 2016 at 02:13 PM 1
Share

Thank you very much for taking the time to give such a detailed answer it really helped me out!

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

47 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

Related Questions

How is it possible to verify values and is they are correct activate objects ? 0 Answers

How to shake the virtual camera when time scale is 0? 0 Answers

Is Time.deltaTime different on various devices? 0 Answers

How to record lap time for player and AIs? 0 Answers

How do I do math in unity.(timer/60 and timer%60) 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