A Simple Wave System On Unity
Tags: programming, unity3d, gamedev
Published: 2021-10-17 16:51:52

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kr6hh4788gn8f8l4bazv.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvcvopif3v3k6vze60c8.png)

**Scriptable Object**

The Scriptable Object named WaveSystemOS is the one that will hold the necessary information to make the waves.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gumac94xzu8ej3zmrxmg.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tw18c2fu21vezxrpn0gp.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zjrpy1h3jmk1si9j4lk1.png)

*WaveSystemManager*

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bqmmx55inmyxbgtwoub3.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/frjcb605nbu84f3iou9y.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u52y6a9pamzzy7trl2ys.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w3xtbomvkkjjg8me64lm.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4kglx2vrbdogx5mme33m.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mppljryyzkqlz4mzqpuw.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5urdj1a6rkh39andcmcn.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8inop2ohzohnznd12hwh.png)


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 😎