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
0
Question by makketronics · Sep 08, 2018 at 04:26 PM · coroutinedeltatimetimers

Unity Timer.deltaTime is consistently inaccurate (reporting higher number),Time.deltaTime reports incorrect (larger) number

Hello,

I have a prefab with the following script:

 public class RefineryScript : MonoBehaviour {
 
     float timer = 0f;
     public float loopRate = 5f;
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
 
         timer += Time.deltaTime;
         if (timer > loopRate)
         {
             Debug.Log("Timer was " + timer);
             timer = 0f;
         }
 }

It is supposed to log every 5 seconds. However, it is logging every 3 seconds. Watch the actual log time in the picture.

alt text

I tried also to implement this in a Coroutine which yields WaitForSecondsRealtime(5f), I also get 3 seconds time delay instead of 5.

Any ideas?

unitytimer.png (35.9 kB)
unitytimer.png (35.9 kB)
Comment
Add comment · Show 2
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 NoDumbQuestion · Sep 08, 2018 at 04:52 PM 0
Share

https://stackoverflow.com/questions/34447682/what-is-the-difference-between-update-fixedupdate-in-unity

avatar image makketronics NoDumbQuestion · Sep 08, 2018 at 05:03 PM 0
Share

$$anonymous$$r. @NoDumbQuestion. The link has nothing to do with this. I am running this on Update, not FixedUpdate, which should be called once a second.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by makketronics · Sep 08, 2018 at 04:58 PM

I counted how many frames in 5 seconds, divided 5 by that number. I get around 300 (302, 298) within 5 seconds.

This means that the Timer.deltaTime "thinks" it is at 60 fps. However, in the stats, I am running around 111fps.

Edit: As per comments below, the issue is with timeScale. Setting timeScale to 1f or using unscaledDeltaTime solved the problem.

Comment
Add comment · Show 5 · 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 NoDumbQuestion · Sep 08, 2018 at 05:22 PM 0
Share

I think you just dont have enough deep understand how Update/FixedUpdate core work within Unity.

Update() is called on FrameRate. Coroutine is called and checked also on Update(). Anything on Framerate will always be fluctuate so dont expecte every frame called the same time. And turn off vsync if you want editor to think it can run above 60 FPS. So if you want precise + consistency deltatime. Then use FixedUpdate() to debug time.

avatar image makketronics NoDumbQuestion · Sep 08, 2018 at 05:39 PM 0
Share

@NoDumpQuestion, Thanks for your reply. However, you are not completely understanding the problem. I am aware that Update() fluctuates. I am simply integrating deltaTime to get total time. For example, if I wait for 10 seconds, I can simply count: 5 + 3 + 3 = 11. In this case, deltaTime is 5, 3, 3. However, Unity is telling me 5, 3, 3 when it should've been 4, 2, or so. Did you look at the log and compare Unity's accumulated time vs actual log time?

avatar image NoDumbQuestion makketronics · Sep 08, 2018 at 05:42 PM 0
Share

Then you forgot to count unscaledDeltaTime.

Show more comments

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

157 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 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 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 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

Trouble with coroutine 0 Answers

Using Time.deltaTime as a Timer 0 Answers

How to force IEnumerator methods to be called only with StartCoroutine 2 Answers

Is there a way to check something every frame from within a coroutine? 1 Answer

Calling coroutine in a method issue. 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