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 randosity · Aug 06, 2012 at 07:34 PM · frameratefunctionsoptimize

run function once per frame

I have a scene that contains ~100 objects with the same script. each script contains a function that takes about .01 seconds to execute. I need the function to execute for every object, but not all at once, so that the framerate isn't murdered. how would i make the objects execute the same function gradually, so that if object A calls a certain function, object B cannot call that same function in the same frame?

sorry if that's confusing.

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

· Add your reply
  • Sort: 
avatar image
3

Answer by ScroodgeM · Aug 06, 2012 at 09:25 PM

solution 1

in each script, add a random framesToSkip value:

int framesToSkip
void Start()
{
    framesToSkip = UnityEngine.Random.Range(0, 50);
}
void Update()
{
    if (framesToSkip > 0)
    {
        frameToSkip--;
    }
    else
    {
        ....do your job
        framesToSkip = 50;
    }
}
this will run once in 50 frames. initialization in start needs so that objects will not do job all at once

solution 2

store all objects on some other empty object with a single script that calls "DoJob()" function on objects with shedule you need

solution 3

use coroutines to make pause you need

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 spalmer · Jun 30, 2015 at 04:10 PM 0
Share

All good ideas. solution 4: keep a centralized frame counter and then check if (frame + objectindex) % objectcount == 0, then run the function, otherwise don't. This is ALU work that doesn't really need to be done. solution 5: put all objects into an IEnumerable. Each frame, somewhere central, maintain an IEnumerator of that. Pick the next object and run its function. If the enumerator is finished, restart it. This is more like a queue and also has overhead, but hardly any ALU.

avatar image
0
Wiki

Answer by IphStich · Jun 30, 2015 at 10:59 PM

I can think of two very clean methods of doing it:

 static int FrameCounter=1;
 
 int myCounter;
 
 void Awake () // Start works as well, but I prefer Awake
 {
     myCounter = FrameCounter++;
 }

 void Update ()
 {
     if (--myCounter == 0) // Works in the order they're created
     {
         // Call Function
     }
 }
   

Or alternatively:

 static bool CanCall = true;
 
 bool HasCalled = false;

 void Update ()
 {
     if (CanCall && !HasCalled) // Whichever instance calls this first gets it first
     {
         CanCall = false;
         HasCalled = true;
         // Call Function
     }
 }

 void LateUpdate ()
 {
     CanCall = true;
 }

Both methods use a combination of static and instance variables to keep track of who should call what when.

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

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

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

Is it worth making tons of similiar methods or create big one that connects things together? 2 Answers

How to debug effectively in release mode? 1 Answer

Refreshrates and vsyncCount, what code should be applied? 0 Answers

Raycast consuming too much ! (VR) 2 Answers

Why is there a 60 fps limit for a PC-Mac build from 2019 onward? 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