In the last tutorial, you wrote the basic properties of your game manager, but the GameManager component doesn’t do anything quite yet. In this tutorial, you’ll add the code to spawn all the aliens.
You could just spawn all the aliens at once, but that would be a boring, albeit overwhelming playing experience. Instead, you’ll spawn a set number of aliens over a range of time. This may sound good, but even this measured approach might not be very fun.
Thankfully, Unity allows you to “tune” your code in real-time. That way, you can see the results as your game is being played versus starting and stopping your game to make slight adjustments. This does pose potential issues that you’ll explore in this tutorial series.
Note: This tutorial is part of a collection that teaches Unity development from the ground up. You can read the entire series over here. This series is free and does not require any account creation. All assets are provided. If you find it useful, feel free to buy me a coffee.
Spawning the aliens
Open the GameManager script in your code editor. Add the following to Update()
:
currentSpawnTime += Time.deltaTime;
currentSpawnTime
accumulates the amount of time that’s passed between each frame update.
Next, add the following:
if (currentSpawnTime > generatedSpawnTime)
{
}
This is your spawn-time randomizer. It creates a time between minSpawnTime
and maxSpawnTime
. You’ll see it come into play during the next change toUpdate()
.
Between the braces, add the following:
currentSpawnTime = 0;
This bit of code resets the timer after a spawn occurs — no reset means no more enemies. Next, add the following:
generatedSpawnTime = Random.Range(minSpawnTime, maxSpawnTime);
This is your spawn-time randomizer. It creates a time between minSpawnTime
and maxSpawnTime
. You’ll see it come into play during the next change toUpdate()
.
Now, add the following:
if (aliensPerSpawn > 0 && aliensOnScreen < totalAliens)
{
}
This logic determines whether to spawn. First, aliensPerSpawn
should be greater than zero, and aliensOnScreen
can’t be higher than totalAliens
. This code is a preventative measure that stops spawning when the maximum number of aliens are present.
Determining the spawn location
At this point in the code, you have determined it is time to spawn an alien. Your next step in the process is to determine where to spawn the alien. There are a lot of spawn points in the game. Ideally, you’ll want to use a “fresh” spawn point. That is, you want to use a spawn point that has already spawned an alien.
Between the braces of your last code, add the following:
List<int> previousSpawnLocations = new List<int>();
This will produce an error since you aren’t using the Collections namespace. Add the following to the top of the code.
using System.Collections.Generic;
The list keeps track of where you spawn aliens each wave. This will be handy so you make sure not to spawn more than one alien from the same spot each wave.
Next, add the following:
if (aliensPerSpawn > spawnPoints.Length)
{
aliensPerSpawn = spawnPoints.Length - 1;
}
This limits the number of aliens you can spawn by the number of spawn points. Add the following underneath the if statement:
aliensPerSpawn = (aliensPerSpawn > totalAliens) ? aliensPerSpawn - totalAliens :
aliensPerSpawn;
This is another chunk of preventative code. If aliensPerSpawn
exceeds the maximum, then the number of spawns will reduce. This means a spawning event will never create more aliens than the maximum amount that you’ve configured.
Creating an alien
Now comes the actual spawning code. Add the following underneath the previous line of code:
for (int i = 0; i < aliensPerSpawn; i++)
{
}
This loop iterates once for each spawned alien. Add the following between the braces:
if (aliensOnScreen < maxAliensOnScreen)
{
aliensOnScreen += 1;
}
This code checks if aliensOnScreen
is less than the maximum, and then it increments the total screen amount. After the aliensOnScreen += 1
, add the following:
// 1
int spawnPoint = -1;
// 2
while (spawnPoint == -1)
{
// 3
int randomNumber = Random.Range(0, spawnPoints.Length - 1);
// 4
if (!previousSpawnLocations.Contains(randomNumber))
{
previousSpawnLocations.Add(randomNumber);
spawnPoint = randomNumber;
}
}
This code is responsible for finding a spawn point.
spawnPoint
is the generated spawn point number. Because it references an array index, it’s set to-1
to indicate that a spawn point hasn’t been selected yet.
- This loop runs until it finds a spawn point or the spawn point is no longer
-1
.
- This line produces a random number as a possible spawn point.
- Next, it checks the
previousSpawnLocations
array to see if that random number is an active spawn point. If there’s no match, then you have your spawn point. The number is added to the array and thespawnPoint
is set, breaking the loop. If it finds a match, the loop iterates again with a new random number.
Now that you have the spawn point index, you need to get the actual SpawnPoint GameObject. Hang in there for a few more steps! You’re close to unlocking an achievement.
Add the following after the closing brace of the while
statement
GameObject spawnLocation = spawnPoints[spawnPoint];
Since there are already assigned spawn points in the Inspector, this grabs the spawn point based on the index that you generated in the last code. Now, to spawn the actual alien. Add the following:
GameObject newAlien = Instantiate(alien) as GameObject;
Instantiate()
will create an instance of any prefab passed into it. It’ll create an object that is the type Object
, so you must cast it into a GameObject
. Now add the following:
newAlien.transform.position = spawnLocation.transform.position;
This positions the alien at the spawn point. Save your code and switch back to Unity.
Select the GameManager and go to the Inspector. Set the Max Aliens on Screen to 10
, Total Aliens to 10
, Min Spawn Time to 0
, Max Spawn Time to 3
, and Aliens Per Spawn to 1
.

Play your game and watch the Scene view. Glorious! Look at all those creepy critters. Granted, they are standing on top of each other. They need to get moving.

Where to go from here
You have the spawning code in place. That’s great! But the aliens don’t do anything yet. It’d be nice for them to actually attack the marine. It’s time for you to get them moving.
This is where you’ll add your enemy AI. Granted, it’s going to be very basic, but it will teach the basics of pathfinding in Unity. You’ll put this in place in the next tutorial.
Discover more from Jezner Blog
Subscribe to get the latest posts sent to your email.