Spawning the Boss

Christian Carter
3 min readSep 22, 2021

--

This tutorial will cover adding the Boss to the Spawn Manager. Start by going to the SpawnManager script. To start you will need to add 3 global variables. The first is a GameObject for the boss prefab. Make sure you make this a serialized field and assign it in the Unity Editor. The second is a bool the will tell the spawn manager if it is on a boss level or not. The third is an int that will keep track of how many boss’ have been defeated. This will be used to increase the health of the boss if multiple boss levels are reached.

[SerializeField] private GameObject _bossPrefab;
private bool _bossLevel = false;
private int _bossCount = 0;

Next, wrap the if and else statement inside the enemy spawn function inside another if statement to check if it is a boss level or not.

IEnumerator SpawnEnemyRoutine()
{
while (_stopSpawning == false)
{
if (_bossLevel == false)
{
... // Enemy spawning
}
yield return new WaitForSeconds(_waitTime);
}
}

Next, update the if statement that checks if all enemies are dead and starts spawning a new wave. The updates you want to make start at the bottom of this section. Add an if statement to check if the wave number subtracting 1 the taking mod 4 of it and check if it is equal to 0. Include a check that the wave number is not 1 so you don’t spawn a boss on the first level. Inside the if statement set that it is a boss level. Then spawn the boss to the top of the screen. Set the bosses health. You will create the function in a couple of steps. Then update the wave in the UI Manager to show “Boss”. If the UpdateWave function in the UI Manager isn’t set to handle strings update it to do so.

if (_numEnemies == 0)
{
_waveNumber++;
_numEnemies = _waveNumber * 2;
_numEnemiesToSpawn = _numEnemies;
_moveType = (_waveNumber - 1) % 4;
_shieldTrigger = ((_waveNumber - 1) % 12) / 4;
_uiManager.UpdateWave(_waveNumber.ToString());
if (_waveNumber != 1 && (_waveNumber - 1) % 4 == 0)
{
_bossLevel = true;
GameObject bossSpawn = Instantiate(_bossPrefab,
new Vector3(0f, 12f, 0f), Quaternion.identity);
Boss boss = bossSpawn.GetComponent<Boss>();
boss.SetBossHealth(_bossCount);
_uiManager.UpdateWave("Boss");
}
}

The last thing to do in the SpawnManager script is to create a OnBossDeath function that is public. In the functions set the _bossLevel variable to false. Increment the boss defeated count. Then update the wave in the UI to the correct number.

public void OnBossDeath()
{
_bossLevel = false;
_bossCount++;
_uiManager.UpdateWave(_waveNumber.ToString());
}

Now go to the Boss script. First add a global variable for the SpawnManager. Make sure you assign it in the Start function.

private SpawnManager _spawnManager;

Next, create the SetBossHealth function that was talked about before. The function should take an int. Inside the function set the health of the boss to 10 plus 5 times the int that is passed to the function.

public void SetBossHealth(int multiplier)
{
_health = 10 + (5 * multiplier);
}

The last thing to do is to call the OnBossDeath function inside the TakeDamage function before the boss is destroyed.

_spawnManager.OnBossDeath();

Now the boss will spawn after four rounds of enemies.

-Chris

--

--

No responses yet