A Simple Wave System on Unity

A Simple Wave System on Unity

·

3 min read

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.
  • 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.

  • 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.

Scriptable Object

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

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.

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

WaveSystemManager

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

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

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

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?

You can create Waves by

#1

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

#2

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

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 😎