Bobblehead Wars is coming along handsomely! At this point, you have a feisty space marine who can turn, shoot and gun down aliens. Also, through the use of a hinge joint, his head can wobble back and forth so he can do so in style.

Nevertheless, Bobblehead Wars isn’t ready for release. First, there is just one alien when the space marine was expecting a horde. And second, the alien currently isn’t that scary — he just stands around!

To fix this, you’ll learn how to spawn enemies over time and make them chase after the space marine, through the power of the GameManager and pathfinding. Let the battle begin!

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.

Introducing the game manager

Unity refers to an object that handles a specific subsystem as a manager. Given your experience in the workplace, you might think this means they do nothing and leave all the work for their underlings, but in Unity managers actually lend a hand. ;]

For instance, an Input System Manager is an object that lets you adjust all the controls for your game. There’s also a SettingsManager that handles all your project settings, and a NetworkManager for managing the state of a networked game.

In this chapter, you’ll create your own kind of manager. Specifically, you’ll create a game manager so that you can easily tweak your game setting while it’s being played. This will make it much easier to balance your game during play testing.

Imagine that you’re playing and there are just too many enemies. The space marine has no chance. You could make an adjustment to your game manager to dial back the aliens hordes in real time.

Remember, any changes you make while your game is played will be lost, right? There’s an exception to this rule that you’ll try out in this chapter.

Creating the game manager

Open your project from Chapter 4, “Physics.” If you need to start fresh, please find the starter project that’s included with the resources for this chapter. You can download the starter project here:

https://drive.google.com/file/d/1j1hO3Iv7lNLUL_H3bzBKo_3xS8vENYVd/view?usp=drive_link

In the Hierarchy, click the Create button, select Create Empty from the drop-down and name it GameManager.

Unity doesn’t provide a “stock” game manager component — it would be silly because each game is unique. You need to write your own.

Select the GameManager in the Hierarchy and click the Add Component button in the Inspector. Pick New Script, name it GameManager then click Create and Add.

This shows the Add Component view with the New Script selected.

Don’t forget to move the script into your scripts folder!

Writing the game manager

Double-click the GameManager script to open it in your code editor. Get ready to spend some time in here; this script will manage all aspects of your game, and you have many variables to add.

Add this code underneath the opening brace:

[SerializeField]
private GameObject player;

[SerializeField]
private GameObject[] spawnPoints;

[SerializeField]
private GameObject alien;

These three variables are critical to the game manger’s function. Here’s a breakdown:

  • player represents the hero’s GameObject. GameManager will use this to determine its current location.
  • spawnPoints are locations from which aliens will spawn in the arena. You declare it as an array because you have multiple locations — remember those capsules you laid out across the arena floor?
  • alien represents the prefab for the alien. The GameManger will create an instance of this object when it’s time to spawn.

Save your script and switch back to Unity. Select the GameManager in the Hierarchy to see three new fields.

This shows the new Game Manager components with the new fields.

First, you’ll populate the player field. Select the SpaceMarine GameObject in the Hierarchy. You’ll notice the Inspector switches from the GameManager to the SpaceMarine, which isn’t what you actually want the engine to do. There are three ways to work around this behavior:

  1. As you’ve done in previous chapters, you could click and drag the object to the property (rather than clicking without dragging). When you click and drag, the Inspector doesn’t select the given object.
  1. A second option is to click the round circle to the right of a property to bring up a popup to select the appropriate object.
  1. A third option is to lock the GameManager to the Inspector. This prevents you from accidentally selecting another GameObject.

Can you guess which option to use?

Locking the GameManager

Locking the GameManager is the best option of the three since you’ve got quite a few changes to make. To do this, select the GameManager in the hierarchy, and click the lock icon at the top of the Inspector.

This shows the Inspector with the locked icon on it.

Select the SpaceMarine in the Hierarchy; the Inspector doesn’t change anymore. In fact, the Inspector will remain locked to the GameManager until you click the lock again.

Drag the SpaceMarine to the Player field. Expand the SpawnPoints GameObject in the Hierarchy. Select all the spawn points by pressing Shift and selecting the top and bottom Spawn objects.

This shows all the spawn points selected in the Hierarchy.

Drag all the spawn points to the Spawn Points property in the GameManager. You’ll know you’re in the right spot when a green plus icon appears.

Next, Drag the Alien prefab from the Prefabs folder (not the Hierarchy) to the Alien field. The GameManager should now look like this:

This shows the GameManager with the properties filled out.

Unlock the Inspector and switch back to your code editor.

Adding some additional properties

Now for some additional properties for your game. Add the following:

[SerializeField]
private int maxAliensOnScreen;

[SerializeField]
private int totalAliens;

[SerializeField]
private float minSpawnTime;

[SerializeField]
private float maxSpawnTime;

[SerializeField]
private int aliensPerSpawn;

These are some variables that you will use to configure gameplay for Bobblehead Wars:

  • maxAliensOnScreen: Will determine how many aliens appear on the screen at once.
  • totalAliens: Will represent the total number of aliens the player must vanquish to claim victory.
  • minSpawnTime and maxSpawnTime: Will control the rate at which aliens appear.
  • aliensPerSpawn: Will determine how many aliens appear during a spawning event.

Next, you need to add a few private variables that the GameManager will need for record keeping. Place the following underneath the public variables:

private int aliensOnScreen = 0;
private float generatedSpawnTime = 0;
private float currentSpawnTime = 0;

Here’s what these will manage:

  • aliensOnScreen: Will track the total number of aliens currently displayed. You’ll use it to tell the GameManager whether to spawn or wait.
  • generatedSpawnTime: Will track the time between spawn events. You’ll randomize this to keep the player on his toes. That way, the player won’t be able to track when the aliens will spawn, making the game harder to predict.
  • currentSpawnTime: Will track the milliseconds since the last spawn.

You now have everything ready for lots and lots of aliens. Lock and load, marine. It’s game time!

Where to go from here

You’ve written the skeleton of your GameManager. You’ll be using this GameObject a lot throughout this game to spawn aliens and even determine the completion of a wave. The key thing to remember is that each GameManager is unique.

Your next step is to spawn aliens based on the values entered into the Unity editor. You’ll do this in the next tutorial.


Discover more from Jezner Blog

Subscribe to get the latest posts sent to your email.

By Brian Moakley

Brian Moakley is a writer and editor who lives amongst the quiet hills in New England. When not reading tales of high adventure, he is often telling such stories to all who will listen.

Related Post

Leave a Reply