Optimizing Coroutines in Unity

Esteban Ibarra
Oct 31, 2021 · 2 min read

Coroutines are extremely useful for any time specific function, however when one uses the yield return new function, that uses a lot of garbage allocation resources as every time the new keyword is used, Unity has to build that new thing on the spot.

This is where caching WaitForSeconds comes in. Think of Caching the WaitForSeconds as a Hot-N-Ready WaitForSeconds routine just waiting for anyone to pick it up and use it!

Since the WaitForSeconds is already built and available, when the coroutine is run, Unity doesn’t have to go through any more extra steps and potentially slow your game down!

The key takeaway here is an important concept in optimizing code is caching your data! Hope it helps!

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Esteban Ibarra

Written by

cartoonist, game artist, and wanabe gamedev.

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.