Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
This question was closed Mar 23, 2019 at 12:16 AM by Bunny83 for the following reason:

The question is answered, right answer was accepted

avatar image
16
Question by James Simpson · May 11, 2010 at 08:28 PM · updatetimedeltatime

Execute code every x seconds with Update()

I want to call a block of code every 0.1 seconds using Update() (assuming that this is the best way to do it), but it tends to run on average every .11 - .12 seconds instead.

 private float time = 0.0f;
 public float interpolationPeriod = 0.1f;
 
 void Update () {
     time += Time.deltaTime;
 
     if (time >= interpolationPeriod) {
         time = 0.0f;
 
         // execute block of code here
     }
 }

             

Comment
Comments Locked
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

4 Replies

  • Sort: 
avatar image
62
Best Answer

Answer by duck · May 11, 2010 at 08:36 PM

See InvokeRepeating. It's designed to do exactly this.

Alternatively, if you particularly want to do it in Update, you could change your code to mark the next event time rather than accumulating the delta time, which will be more accurate:

 private float nextActionTime = 0.0f;
 public float period = 0.1f;
 
 void Update () {
     if (Time.time > nextActionTime ) {
        nextActionTime += period;
         // execute block of code here
     }
 }
             

Comment
Comments Locked · 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 tatoforever · Dec 04, 2014 at 11:52 AM 9
Share

This does not work. Time.time will always be higher than nextActionTime thus the "execute block of code here" will be executed every frame. It should rather be: void Update () { if (Time.time > nextActionTime ) { nextActionTime = Time.time + period; /execute block of code here/ } }

avatar image sapirnoam · Mar 22, 2019 at 03:33 PM 0
Share

Thank you very much!

avatar image
14

Answer by marchall_box · Nov 07, 2016 at 07:40 AM

How about running a coroutine function?

 IEnumerator DoCheck() {
     for(;;) {
         // execute block of code here
         yield return new WaitForSeconds(.1f);
     }
 }

and run this in your start or something

 StartCoroutine("DoCheck");

This will let the function to run every tenth second. You can change the argument to change time. Hope that helps.

Comment
Comments Locked · Show 4 · 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 Rad-Coders · Mar 29, 2017 at 06:07 PM 0
Share

Thanks, works like a charm.

avatar image AlonM99 · Apr 04, 2017 at 06:31 PM 0
Share

what is the "AFunc" ?

avatar image Bunny83 AlonM99 · Apr 04, 2017 at 06:52 PM 0
Share

Well, simply the code you want to execute. He just calls an arbitrary method here. I'll edit the answer so it follows the same "style" as the other answers.

avatar image Pengocat AlonM99 · Apr 04, 2017 at 07:15 PM 0
Share

AFunc is just any function you want to call I guess.

Using "new" very often is going to generate garbage however.

     WaitForSeconds waitForSeconds = new WaitForSeconds(0.1f);
 
     IEnumerator Start()
     {
         while (true)
         {
             // Place your method calls
             yield return waitForSeconds;
         }
     }

Something like this should be more memory friendly.

avatar image
4

Answer by Joe-Censored · Apr 05, 2017 at 01:03 AM

If you like doing it in update, I would just do a 1 line change to below.

This way if you hit update when time is at 0.11f, you subtract 0.1f and are already at 0.01f before getting to your next update. You'll average out a lot closer to 0.1f in the long run than your original implementation.

 private float time = 0.0f;
 public float interpolationPeriod = 0.1f;
 
 void Update () {
     time += Time.deltaTime;
 
     if (time >= interpolationPeriod) {
         time = time - interpolationPeriod;
 
         // execute block of code here
     }
 }

Comment
Comments Locked · 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
3

Answer by ATeyken · Dec 16, 2016 at 03:06 PM

This works for me,

 public float period = 0.0f;
 void Update()
 {
     if (period > [Time Interval])
     {
         //Do Stuff
         period = 0;
     }
     period += UnityEngine.Time.deltaTime;
 }

Comment
Comments Locked · 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

10 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

Related Questions

Single Step (pause, resume), or externally clock game loop 0 Answers

Time.deltaTime not consistent over time 1 Answer

Time.deltaTime not working correctly 7 Answers

Add 1 Per Second to an Int 1 Answer

Movement using Time.deltaTime not working on Fast mac 3 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