- Home /
Confused about using Update vs. Coroutines
Researching this topic, I came to these two conclusions.
Coroutines are great for timed events
Updates() and Coroutines behave exactly the same performance-wise. As in, the less Updates called and the less Coroutines running at any given time frame, the better performance you'll have (not to generalize, but removing code from being regularly updated is more significant than how it's being called)
That being said, I'm slightly stuck on how yielding works. Is this how it works?
Frame 1: Unity main thread runs FunctionA
Frame 1: FunctionB calls coroutine FunctionB
Frame 1: FunctionB yields
Frame 1: Since FunctionB yields and not finishes completely, FunctionA yields to Unity main thread
Frame 1: Unity main thread gets control, moves onward with rendering
Frame 2: Unity main thread notices FunctionA coroutined Function B
Frame 2: FunctionB resumes control at point it left off at, function finishes out normally
Frame 2: FunctionA gets resumed at point where it coroutined FunctionB, finishes out normally
Frame 2: Unity main thread gets control, moves onward with rendering
Frame 3: Unity main thread runs FunctionA
Frame 3: FunctionA finishes out normally
Frame 3: Unity main thread gets control, moves onward with rendering
...etc...
If this is how yield works, than I can easily understand how this would be useful. If not, I need assistance!
http://answers.unity3d.com/questions/56415/increasedecrease-value-with-time-problem.html
http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html
http://answers.unity3d.com/questions/119076/coroutines-vs-updatefixedupdatelateupdate.html
Answer by Paulius-Liekis · Jul 30, 2012 at 12:19 PM
Is function FunctionA a coroutine? If not, then this is not happening: "Frame 2: FunctionA gets resumed at point where it coroutined FunctionB, finishes out normally".
There several ways to call yield and StartCoroutine. I find explicit syntax in C# much more clear, because Javascript ^tries to help you^, i.e. if it finds any yield statements in your function then it converts it to coroutine...
Calling "StartCoroutine(FunctionB())" from FunctionA (where FunctionA is a couroutine or not - doesn't matter) will start FunctionB and then continue executing FunctionA without waiting for FunctionB to complete
Calling "yield return StartCoroutine(FunctionB())" from FunctionA (where FunctionA is a couroutine) will start FunctionB and FunctionA will continue only after FunctionB is complete
Calling "yield return null" from FunctionB (where FunctionB is a couroutine) will return to main thread and then continue FunctionB in next frame
BTW: Coroutines do not give you performance, they are just convenient when you need to delay something and similar situations...
Ok, thank you for the clarification. This makes more sense now.
Answer by IndieForger · Feb 17, 2013 at 04:24 AM
Biggest difference is that update is executed every frame and coroutine is basically task which can be delayed. I think it depends where you want to use it.
If you lets say cleaning particle system after it has finished rendering particles you should use coroutine in most cases along with IsAlive method. However if you have many particle system you should use update method in external controller that will keep an eye on all particle systems.
I wrote about it here
I did some test and coroutine might give you performance but should be used wisely. Managing 1000 coroutines against 1000 updates is not the best idea but then even 1000 updates doesn't make sense if all the calculations could be done in 1.
Your answer
Follow this Question
Related Questions
Alternative for semaphores in Unity? 2 Answers
Threading in Unity 6 Answers
Invoke repeating OR coroutine with WaitForSeconds? 1 Answer
Is there a yield WaitForSeconds type code for the update? 2 Answers
Im a bit confused on a simple script 1 Answer