- Home /
What's the cheapest way to run a method every few frames?
I am looking at preforming some things that don't need to be called on Update but around 10 times a second, I know Fixed update tries to run at 60 frames unlike Update that tries to run however many frames it can. I am not too excited about coroutines either. What's the most efficient way to preform a few tasks a second?
Answer by 5c4r3cr0w · May 05, 2016 at 05:34 AM
You can use InvokeRepeating() function that you have to call in Start() and pass the time duration to repeat itself.
Ref : http://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html
Thanks, but InvokeRepeating is basically "WaitForSeconds" That is actually more expensive with it's yeilds.
I am looking for the absolute cheapest way to call these functions.
Really? Have you tested it? Because in every test I've seen, InvokeRepeating is slightly more efficient than yielding with WaitForSeconds. And seeing as you've already ruled out using coroutines (why?), you're pretty much out of options...
FixedUpdate() runs however often you tell it to based on the physics update timestep - it doesn't have to be 60FPS - but I wouldn't normally recommend changing this just for the purposes of scheduling a function.
I'd just go with @5c4r3cr0w's suggestion, or provide more details as to why that's not appropriate for your use-case.
InvokeRepeating has to use reflection: https://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.invoke(v=vs.110).aspx
I never said I was ruling out coroutines I said I was not that excited about them.
Your answer
Follow this Question
Related Questions
How efficient is Update() vs Update via a Manager vs Coroutines for iOS Deployment? 2 Answers
Update scene every 5 minutes 2 Answers
Simple Coroutine doesn't finish 1 Answer
inconsistent results calculating the distance moved by an object 1 Answer
Could Update and FixedUpdate happen at the same time? 1 Answer