Spawn Manager and Coroutines with Unity
In every game you need to spawn certain objects, these can be enemies or items to collect. The best way to do this is to create a spawn manager.
Create a Spawn Manager Object and the Script
First create an empty object and name it Spawn_Manager, then create a script, name it SpawnManager and add the script to the object (drag the script and drop it onto the spawn manager object).
Coroutine and Instantiate
Our task is to spawn the object every 5 seconds from a static vertical coordinate and a random horizontal coordinate. Take a look at the code below.
The code above has a few extra lines of code responsible for spawning objects in the subfolder of the hierarchy window or stopping the spawning when the player dies. Couroutines with an infinite loop are very useful if you want to repeat a task at regular intervals, i.e. do and wait and do it again.