DEV Community

Cover image for A Simple Wave System On Unity
Esther
Esther

Posted on

A Simple Wave System On Unity

GitHub [ https://github.com/rehtse-studio/SimpleWaveSystem ]

In this short article I’m going to walk you through on how the SimpleWaveSystem project works.

The Unity project is very easy to navigate; it was done using Unity 2019.4.29f and in the Hierarchy windows we have a GameObject label WaveSystem that holds the necessary GameObjects that have the scripts to control the wave and a simple UI Canvas to display some text and buttons to start and restart the wave.

Image description

On the project window there’s a folder named SimpleWaveSystem, this folder has all the scripts, scriptable objects and basic enemy game objects to test and make the wave system work.

Image description

Scriptable Object

The Scriptable Object named WaveSystemOS is the one that will hold the necessary information to make the waves.
Image description

The script itself has a class called WaveSetUp, inside a class we have a public int property that will hold the ID of the GameObject you want to spawn and a public GameObject property to hold your Enemy or item game object.

This class is turned into an array by calling it in the WaveSystemSO that inherits from ScriptableObject. By creating this array you can control what types of items or enemies are created on a specific wave and how many. We have an int that will determine how many items or enemies are being spawned in this wave.
Image description

Have in mind that regardless of how many objects you’re using on the wave if the amountToSpawnOnThisWave variable is not higher than the items, all the items will not be used.

The ReturnObjectType() method will be called by the SpawnManager; the method will return the ID of the object and the SpawnManager will use this ID to call the necessary object from the PoolManager.

Managers

Image description

WaveSystemManager

Image description

This script is where you’re going to hold the reference of each wave you create. The WaveSystemManager is the one that is going to share the necessary properties to the PoolManager and to the SpawnManager. By having just one script to share this type of data you can change the wave at any giving moment and the system will still work.

PoolManager

Image description
The PoolManager is going to create a list of the necessary objects you put on the waves; this list is put inside a Dictionary, it will use the objects ID to use it as the Key Reference to later call a specific list object. For each list the PoolManager will create 20 of each object, by doing this the SpawnManager will spawn the object of the select list. Keep in mind that you can change the pool size on the script; if you increase the spawn item on the wave and it is higher than the pool size you must increase the pool size.

SpawnManager
Image description

Image description

Image description

The SpawnManager is the one that will call the object using the PoolManager and it will also control the time between each spawn object or the time between each wave. This script controls the position on where you want the object to spawn; you can change the position to your linking using the public method called ObjectPositionToSpawn().

Reminder
Image description
Since the object (item, enemies, etc) are spawn using the PoolManager and can be reused; remember to turn off (SetActive(false)) the object, do not destroy.

How To Create Waves?

1

Right Clicking on the Project Window -> Create -> Rehtse Studio Simple Wave System -> Create New Wave

Image description

2

Assets -> Create -> Rehtse Studio Simple Wave System -> Create New Wave

Image description

Hope you like the repo, if you see something strange or a error please let me know and you can make whatever changes you want on the code😁

To be continued 😎

Discussion (0)