• 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 Sep 27, 2019 at 06:41 PM by hexagonius for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by chelder · Oct 19, 2015 at 10:15 AM · optimizationupdatefixedupdateinvokerepeatingoptimize

Optimization: how to do a checking less often than within an Update

Right now, this is my code (it is working):

 void Update() {
     if (Cardboard.SDK.Triggered) {
         Application.LoadLevel(Application.loadedLevelName);
     }
 }

However, it is not needed to perform that check every frame. I'm trying to use the Update function as less as possible. It would be OK to perform that check every 0.5 seconds for instance. I have found the following possibilities:

  • InvokeRepeating(): I wonder if it would increase the performance somehow, or it uses Update internally.

  • FixedUpdate() after configuring Edit -> Project Settings -> Time -> Fixed Timestep: it would change the fixed timestep for the whole game, so I think it would not be a good idea.

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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by hexagonius · Oct 19, 2015 at 10:53 AM

Use either invoke repeating, or your own Coroutine with a while true and WaitForSeconds in it. In your case performance won't be an issue at all though. Rule of thumb, develop first, find bottlenecks last.

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
5

Answer by Michael_Berna · Sep 26, 2019 at 09:18 PM

In case anyone else finds this old thread, here's a great method that helped me. Directly from Unity documentation:

Run code every [x] frames

If code needs to run frequently and cannot be triggered by an event, that doesn't mean it needs to run every frame. In these cases, we can choose to run code every [x] frames. it would be sufficient for our needs to run this code once every 3 frames. In the following code, we use the modulus operator to ensure that the expensive function runs only on every third frame.

 private int interval = 3;
 
 void Update()
 {
     if (Time.frameCount % interval == 0)
     {
         ExampleExpensiveFunction();
     }
 }

An additional benefit of this technique is that it's very easy to spread costly code out across separate frames, avoiding spikes. In the following example, each of the functions is called once every 3 frames and never on the same frame.

 private int interval = 3;
 
 void Update()
 {
     if (Time.frameCount % interval == 0)
     {
         ExampleExpensiveFunction();
     }
     else if (Time.frameCount % interval == 1)
     {
         AnotherExampleExpensiveFunction();
     }
 }

Original document: https://unity3d.com/es/learn/tutorials/topics/performance-optimization/optimizing-scripts-unity-games

Comment
Add comment · 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 aaronrums · Apr 09, 2021 at 10:22 AM 0
Share

this is a great tip, thank you!

avatar image LandePlage · May 19 at 09:02 PM 0
Share

To be even more performant, you could use "FixedUpdate" instead of "Update", if possible.

Follow this Question

Answers Answers and Comments

34 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

Related Questions

How to handle FixedUpdate() and Input 0 Answers

How to optimize a PC game? 1 Answer

GetButtonUp works but not always 0 Answers

Addforce in fixedupate 0 Answers

Debris particles slowing down simulation 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