Creating a Spawn Manager for a Continuous Flow of Enemies

David Hunter Thornton
3 min readAug 22, 2022

--

How do I make my enemies spawn into the game without having them already in the scene?

Objective: Explain how to set up and write a proper Spawn Manager for our enemies and mines.

In my last article we finalized all of the basic behavior for our Player and Enemies. In this article we’re going to be talking about Coroutines again to create a Spawn Manager.

The very first thing that I want to do is create an Empty Game Object and name it “SpawnManager”. Empty Game Objects function the same as any other Game Object. The only difference being that it doesn’t have any components aside from “Transform” which all Game Objects have.

An Empty Game Object basically doesn’t exist in your game. Its very useful to create anchor points for other objects, UI, or to attach a script so that it stays active in your game at all times.

For this, we’re just going to attach a script to it.

Let’s go ahead and setup the basics for our Coroutine with an IEnumerator and have it wait for 5 seconds.

Additionally we need to create a SerializeField for Enemy Prefab so that our script knows what to Instantiate.

And before you forget, drag the Enemy Prefab and drag it into this new field in the Inspector.

And also create the basics of our method. Again, everything I’m showing has been covered here.

The next part gets kind of tricky though so be careful. We’re going to create an infinite “while loop”. While loops are almost like an if statement that’s asking, “if this is true, continue to do this”. So if you write a while loop that is always true, then it will continuously trigger every single frame. This can easily crash Unity or your PC if not implemented properly.

In the above, I’ve also repeated the random x position code from this article.

The only thing left for my project is to repeat this for the Mine Enemy with a slightly different spawn speed for variety.

Friendly Reminder: Don’t forget to keep updating your project through GitHub/Git and saving any changes you make to a separate branch. You can check out my other articles to get a breakdown of those steps! Day 1–9

--

--

David Hunter Thornton
David Hunter Thornton

No responses yet