Enemy Waves with Scriptable Objects

Hal Brooks
3 min readMar 13, 2022

--

Objective: Create waves of enemies using scriptable objects.

Three waves of enemies are spawned, each with three different enemies.

Create a new scene and add a plane for the ground. Now add a Cube, Cylinder and Sphere to the scene, tagging these as Enemy. Drag these enemies into your Prefabs folder and delete from the hierarchy. Next create a new script named Wave which inherits from ScriptableObject. The CreateAssetMenu allows a newWave to be generated by right clicking on a folder in the project’s Assets to create ScriptableObjects > Wave. Wave contains the waveSequence, which is a list of GameObjects to be spawned for that wave. Create three newWaves: renaming Wave_0, Wave_1 and Wave_2.

Wave scriptable object

The order for enemies in each wave can be assigned by selecting, for example Wave_1, and dragging the enemy prefabs into the waveSequence. Repeat this process for Wave_0 and Wave_2. Three are used for each Wave in this example, but any number of enemies could be used.

Wave_1 enemy sequence.

Next create an empty game object and name it Spawn_Manager then add a new script called Spawner. This script requires five private variables: _spawnName, _enemyWave, _spawnPoints, _currentWave and _instanceNumber.

The variables _spawnName, _enemyWave, _spawnPoints all utilize SerializeField so that they can be assigned in the inspector as shown below. The _enemyWave is a list of Wave scriptable objects. SpawnPoints is an array of Vector3 positions. The _spawnName is the name that will be used for instantiated objects.

Spawner variable assignment within Spawn_Manager.

The Spawner script then starts the StartWaveRoutine, which manages enemy waves. While the _currentWave is less than the _enemyWave.Count, another IEnumerator, SpawnEnemiesRoutine, is used to generate enemies within the wave. After 5 seconds all activeEnemies are found using their tag and destroyed using a foreach loop. The _currentWave is incremented, eventually exiting the while loop after all waves are completed.

IEnumerator StartWaveRoutine in Spawner script

The SpawnEnemiesRoutine() begins by setting the currentSpawnPointIndex to zero. A for loop is started for the length of the waveSequence for the _currentWave, which instantiates each enemy GameObject obj to currentEntity and renames it using the _spawnName + _instanceNumber. The currentSpawnPointIndex and _instanceNumber are then incremented. The routine then yields for one second before proceeding.

SpawnEnemyRoutine within the Spawner script.

The use of a Wave list, _enemyWave, within the Spawner allows each _enemyWave to be assigned in the inspector. Each Wave is itself a list which is assigned in the inspector using a scriptable object. Scriptable objects allow a list of lists, all assigned in the inspector.

--

--

No responses yet