Coroutines with Unity

Declaring a coroutine

To declare a coroutine, we use the IEnumerator interface type and the function’s name

Calling a coroutine

A way to call a coroutine is using the StartCoroutine( ) function, and in the parenthesis, we write the coroutine’s name between quotation marks

  • In Start function, assign the SpawnManager script to the variable spawnManager using GameObject.Find and GetComponent functions
  • If lives = 0, call OnPlayerDestroyed in SpawnManager script (see below) The function will change the isPlayerAlive value
  • Change the while condition value (true) with the new bool (isPlayerAlive)
  • Create a public function in SpawnManager class (for example, OnPlayerDestroyed) to modify the isPlayerAlive value



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