Step 5 — Enemy Spawning With Coroutines
At this point we want our enemies to keep spawning in at a set interval of time.

We shall accomplish that using Coroutines. Think of them as specialized methods, with the bonus that you can tell it to wait for a period of time before continuing.
To begin, create an empty game object in the Scene, and call it SpawnManager. Give it a script of the same name, and add the following.

You can set the _enemyPrefab in the Unity Inspector, and the _isPlayerDead bool is important to tell the spawn coroutine to stop.
The coroutine is the one that starts with IEnumerator, and instead of returning a data type or void, it has to have a yield return.
We enclose our code to Instantiate enemies inside a while loop, which will continue to run the code block it encloses so long as its condition is true.
Note:
(!_isPlayerDead) is the same as (_isPlayerDead == false)
After Instantiating an enemy at a random x coordinate, the yield return new WaitForSeconds(3f) tells this coroutine to pause for 3 seconds, before continuing the loop again.
In order for all the above to run you would need to actually start the coroutine. Starting the coroutine is a little different than calling methods as normal. You will need to call a special StartCoroutine method and pass in the particular coroutine you want to run.

Lastly about the PlayerDied() method. That is for when the player dies, the Player object calls this SpawnManager’s PlayerDied() method, and sets _isPlayerDead to true, stopping the coroutine while loop.