Which is more efficient, Update() or a couroutine with while(true)?
So, I'm just a noob developer and I've been working on a project which was already in development before I joined. And instead of using Update(), coroutines are used in many places.
So I was just wondering which is more efficient. An Update() or a coroutine which looks tlike this:
IEnumerator MyUpdate()
{
while(true)
{
yield return new WaitForSeconds(Time.deltaTime);
//Update things here.
}
}
a co-routine like that doesnt execute every frame like Update() does the below would be the same as Update() method
IEnumerator myUpdate()
{
while (true)
{
yield return new WaitForEndOfFrame();
print ("hello");
}
}
but still its just extra code i think Update function would be more efficient although im a newbie as well in unity and c#
$$anonymous$$ight put this as answer, since it's a very solid answer.
That's actually wrong. This coroutine would not behave the same as Update. It would be the same when you yield return null;
ins$$anonymous$$d. WaitForEndOfFrame let you execute code at the very end of the frame, even after rendering has completed. Update runs more or less "in the middle" of the frame.
^^ I second this. from @Bunny83
Highly useful to look into: https://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html
Answer by Kyle_WG · Mar 16, 2017 at 01:56 PM
Coroutines are best when you have a specific condition to wait for. As in; waiting for a threaded job to finish or in this case waiting for a certain amount of time. Coroutines also generate garbage, especially when you're yielding with yield return new WaitForendofFrame()
As every time you yield that object is created.
Unity, internally, holds a list off all things needing to update essentially and traverses that when everything needs to update. Definitely more advantagous as there's features to explicitally state execution order.
I recommend this entirely over coroutines.
I highly recommend learning this Script Lifecycle Flowchart. It will show when your code is executed within the engine and what the different yield types are. It'll help you keep game, physics and rendering logic in their right places.
https://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html
It is true that WaitForEndOfFrame and WaitForSeconds are objects which generate garbage each time you create a new instance. However those can be simply cached and reused in which case it doesn't behave much different from Update. There might be some additional overhead involved but it's generally not that much to even think about.
Coroutines, as you said, let you wait for specific "events" which Update can't do at all. For example WaitForEndOfFrame. This let you execute code after the rendering of the current frame has completed this can't be done with Update. It can be done with some of the Camera callbacks, but for this the script need to be attached to the camera.
Where coroutines are nice as well is when you need to perform a task every frame for a limited time period. When using Update you either have to use a boolean flag or any other mechanic that prevents the code from executing when not needed, or you have to disable the whole script.
Your answer
Follow this Question
Related Questions
Using Coroutines and Update together 0 Answers
How to properly call Async Task 0 Answers
loop inside update 0 Answers
Unity C# Punching Coroutine not completing 0 Answers
Why Unity docs do not use New with WaitForSeconds and yield? 1 Answer