In many cases you might make a coroutine that affects game objects in your scene, like one that moves a button from point A to point B over time. MEC coroutines don’t automatically stop running when the gameObject that they were created on gets destroyed or disabled like Unity’s coroutines do. The reason that this behavior is not included in MEC by default is because it doesn’t always make sense for all coroutines. If your coroutine doesn’t work on UI elements then at best this check is a waste of processing and at worst it can lead to undesired behavior.

When you’re working with UI elements it’s a good idea to turn this check on so that you don’t get errors when changing screens. With MEC you do that using the CancelWith extension, like this:


TLDR: You should use .CancelWith on all coroutines that affect UI elements.

CancelWith does increase the size of the unavoidable GC alloc that all coroutines generate by approximately 20 bytes. 20 bytes isn’t large, but if you want to avoid every possible GC alloc then you can relatively easily do what CancelWith does without using the function. Just make sure that you make the following check after every yield return statement inside your coroutine:

if(gameObject != null && gameObject.activeInHierarchy)