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
0
Question by hubert322 · Mar 28, 2015 at 04:46 PM · timerframe

Little Error on Timing

I am trying to make a timer and display the time, and I've got it. However, there would be a little bit of error. alt text

As you can see from the image, my timer starts at 60 (that is not the problem), but when it jumps from 60 to 59, it is faster than others. For example, 59 to 58 would be slower (in the real time seconds), and 60 to 59 would be faster by some. I think it is problems about frames, but I am not sure how I can solve this. Here are my codes:

 public class Timer : MonoBehaviour 
 {
     public Text text;
 
     float timer;
 
     void Awake ()
     {
         timer = 60;
     }
 
     void Update ()
     {
         if (Time.timeScale != 0)
         {
             timer -= Time.deltaTime;
             text.text = "Time: 0:" + timer.ToString ("00");
         }
     }
 }


 public class TimeDelay : MonoBehaviour
 {
     float theTime;    
 
     void Awake ()
     {
         Time.timeScale = 0;
     }
 
     void Update ()
     {
         if (Input.GetKey (KeyCode.A))
             Time.timeScale = 1;
     }
 }



untitled.png (20.4 kB)
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
0
Best Answer

Answer by NoseKills · Mar 28, 2015 at 08:33 PM

The formatting timer.ToString ("00"); rounds the number to nearest integer, so 60 is displayed only from 60 to 59.5 seconds ( and btw the zero would be displayed for 0.5 seconds before time actually hits zero).

You might want to do

 Mathf.CeilToInt(timer).ToString ("00");
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 hubert322 · Mar 29, 2015 at 02:03 AM 0
Share

Thank you! That solved my problem thanks!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Frame dependant timer 1 Answer

more resource efficient TIMER 2 Answers

how to make a varied countdown timer 1 Answer

Timer script is turning minutes into seconds 2 Answers

How do I put a delay in this script ? 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