# Function not waiting

**URL:** https://discussions.unity.com/t/function-not-waiting/1525426
**Category:** Desktop
**Tags:** Windows, Scripting, Windows-Editor, Question
**Created:** [September 25, 2024, 12:05pm UTC](https://discussions.unity.com/t/function-not-waiting/1525426 "2024-09-25T12:05:21Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![Kurt-Dekker](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kurt-dekker/32/101578_2.png) [@Kurt-Dekker](https://discussions.unity.com/u/Kurt-Dekker)
#### Post date: [September 25, 2024, 2:07pm UTC](https://discussions.unity.com/t/function-not-waiting/1525426/5 "2024-09-25T14:07:01Z")

</div>

> [@C3k456](#):
>
> I just simply didn’t have the time to do in depth research about coroutines.

I would recommend against using things you don’t have the time to understand. 🙂

Coroutines in a nutshell:

> [@Proper way to stop coroutine?](https://discussions.unity.com/t/proper-way-to-stop-coroutine/749264/9):
>
> Coroutines are an IEnumerator object. They have their own internal state. It’s different from just calling InvokeRepeating(), which would restart a fresh function call each time. Here is ALL you need to know about coroutines in Unity: they are an IEnumerator, also known in some languages as a Generator. It is an object with a .MoveNext() method on it that “yields” the next thing with each subsequent call. That is what you create by calling an IEnumerator: you create the object, you do NOTHIN…

> [@Destroying the object then restarting the scene](https://discussions.unity.com/t/destroying-the-object-then-restarting-the-scene/825667/6):
>
> Hey Epoch, here’s how it goes down. A coroutine is just an IEnumerator object, and you make one by just calling a coroutine. When you do that, nothing happens. It’s like a water pump that needs to have its crank pumped, but nobody is pumping it so it sits there and does nothing. In Unity there’s a special agreement: if you hand that IEnumerator object into StartCoroutine(), which is a method belonging to the object where that StartCoroutine() call is located, what Unity does is starts “pumpin…

Splitting up larger tasks in coroutines:

> [@Best Way to Split a Long-Running Function So it's Non-Blocking?](https://discussions.unity.com/t/best-way-to-split-a-long-running-function-so-its-non-blocking/840656/2):
>
> Technically, yeah, there’s always a way. But in practice, in the general sense it can get really hairy. Also, in the particular case of adding lots of GameObjects, I know they’re not linear: last I checked, adding 1000 GameObjects is WAY more than 10x slower than adding 100 Gameobjects, I presume due to inefficiencies in newly-added stuff, not sure/ No way to tell with a closed-source engine like Unity. Trying to tie into system timers and judge how long things have taken so far will get you …

Coroutines are NOT always an appropriate solution: know when to use them!

“Why not simply stop having so many coroutines ffs.” - orionsyndrome on Unity3D forums

> [@Things to check before starting a coroutine?](https://discussions.unity.com/t/things-to-check-before-starting-a-coroutine/857304/4):
>
> The main thing to check before calling / making a coroutine is to ask yourself, “Does this even need to be a coroutine?” Generally, if your design requires you to call StopCoroutine(), then don’t make it a coroutine. If you just need something to happen a bit later, this is a more-robust approach: [https://gist.github.com/kurtdekker/0da9a9721c15bd3af1d2ced0a367e24e](https://gist.github.com/kurtdekker/0da9a9721c15bd3af1d2ced0a367e24e) If you’re flipping from one value to another, such as fading in, fading out, changing speeds slowly, etc., coroutines are an AW…

> [@Heartbeat courutine fx](https://discussions.unity.com/t/heartbeat-courutine-fx/849607/4):
>
> Again I would not reach for coroutines here. The reason: you can go below the critical health, then go up, then go back down, and every single point in there is a chance to screw up how many coroutines are actually running. Coroutines are great but they are the wrong solution for this problem. Instead, make a heartbeat script and have a variable to count if it is time yet: private float heartBeatYet; In your Update() function, calculate the desired rate of heartbeat based on health: float …

Our very own Bunny83 has also provided a Coroutine Crash Course:

> [@Coroutines IEnumerator not working as expected](https://discussions.unity.com/t/coroutines-ienumerator-not-working-as-expected/237287/3):
>
> slight_smile I’m kinda suprised that until now nobody has asked a question like this. Even though I have explained what a coroutine actually is several times in other questions it just shows that there is still quite a misunderstanding what a coroutine actually is. Lets just step back for a moment. Unity implements coroutines by utilizing a C# feature called “iterator blocks” or iterator methods (which are more generally known as [Generator methods](https://en.wikipedia.org/wiki/Generator_(computer_programming))). C# ships with two relevant interfaces (IEnu…

And another overview by Anders Malmgren:

> [@I'm addicted to using Coroutines. Any alternatives?](https://discussions.unity.com/t/im-addicted-to-using-coroutines-any-alternatives/1498556/6):
>
> You need to understand the basics of what the Unity coroutine system is based on. Its not magic, its based on the fundamental basics of C# 3.0 which introduced the iterator pattern as part of their Linq feature. Any method that returns IEnumerator or IEnumerator\<T\> or IEnumerable/\<T\> can be iterated upon. This is actually a very easy concept. The IEnumerator interface as follows public interface IEnumerator { bool MoveNext(); object Current { get; } void Reset(); } Its…

---

_[View the full topic](https://discussions.unity.com/t/function-not-waiting/1525426)._
