Coroutines with Unity!

As an alternative to the Update method, there is another way to create code loops in tandem with the Update method. This is useful when code needs to execute independent of the frame-by-frame procedural execution of the Update method without delaying any other processes.

To call a coroutine in Unity we use StartCoroutine(). A coroutine returns a value of type IEnumerator. In this example, we’ve created our IEnumerator coroutine to spawn a new enemy every second:

This while loop will never complete so this coroutine will never end, but at the same time does not prevent us from executing other methods simultaneously from the Update method.

Using coroutines in Unity gives us more control over the lifecycle of the execution of a method.

--

--

--

Unity and Flutter developer interested in creating mixed-reality applications.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Six tools to Simplify Kubernetes — Journey — Day 5 — Kubewatch — A watcher for Kubernetes

How did I design my CV in LaTeX

The CSS Grid Terminologies you Should Know

How we took onboarding to the next level at Stuart

Using different Firebase environments in an iOS app

Docker Networks — Simplified Part-1

Spring Boot — Kotlin — Getting start unit testing

Webtoon CLI Gem

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Richard Morgan

Richard Morgan

Unity and Flutter developer interested in creating mixed-reality applications.

More from Medium

How to Play Sound Effects in Unity

Unity — Final Boss big sweeping laser

Static Libraries in C