Enemy wave system
Objective: Add wave system that adds enemy types and increase score with each added wave.
First create UI text object and name it Wave_text. Set the position on the UI canvas and anchor it to the upper right. Change the font size to 20 and the color to white, as seen below.
In the UIManager script, create a new [SerializeField] variable private Text _waveText, and assign the Wave_text above to this new variable in the Unity inspector. Create a public void UpdateWave() function to allow the SpawnManager script to pass the current wave to the UIManager.
The GameManager script with tracks the wave with private int _wave using the function UpdateWave() as shown below. It also allows access to the current wave with the CurrentWave() function.
Since the wave controls the enemies being spawned, the Spawn_Manager script needs to track several new variables shown below. The _wave is initially 1. The variable _enemiesSpawned is used to track advancement to the next wave, and _endWave is the number of enemies spawned required to advance to the next wave. The _uiManager allows the UI Wave_text display to be updated using UpdateWave() function. The current wave is sent to the _gameManager using UpdateWave() function.
In the SpawnManager’s IEnumerator function, the _enemiesSpawned++ increments with each new enemy. When the _enemiesSpawned is greater than or equal to _waveEnd, initially 10, then the difficulty is increased via _wave++. The UIManager and GameManager are updated and _waveEnd is increased to define the next wave threshold. Waves 2–5 are triggered at 10, 20, 35 and 55 enemies spawned.
There are currently five enemy types, each with increasing difficulty. In wave one the basic enemy that moves back and forth and occasionally fires is spawned. On wave 2 another enemy with less predictable motion can also spawn. To do this the enemy script must access the wave which is in the manager script using the line of code below in void Start().
The type of enemy spawned is controlled by a random number which is generated based upon the _wave, but clamped to the number of enemy types in the game.
Finally, the score awarded for killing each enemy is increased with each wave to reflect the increased difficulty.
By wave five, all the enemy types are in play.