::In Part 25 of the series, we added a reaper to our game, and in the GameManager, we disable the spawn manager when time runs out on the stopwatch.
A minor optimisation can be made on the UpdateStopwatch() function of GameManager — instead of using FindObjectOfType() to get a reference, we can use SpawnManager.instance, as the class automatically assigns a static reference of itself for convenience.
void UpdateStopwatch()
{
stopwatchTime += Time.deltaTime * ClockSpeed;
UpdateStopwatchDisplay();
if (stopwatchTime >= timeLimit && !levelEnded)
{
levelEnded = true;
// Set the enemy/event spawner GameObject inactive to stop enemies from spawning and kill the remaining enemies onscreen.
FindObjectOfType<SpawnManager>().gameObject.SetActive(false);
SpawnManager.instance.gameObject.SetActive(false);
foreach (EnemyStats e in FindObjectsOfType<EnemyStats>())
e.SendMessage("Kill");
// Spawn the Reaper off-camera
Vector2 reaperOffset = Random.insideUnitCircle * 50f;
Vector2 spawnPosition = GetRandomPlayerLocation() + reaperOffset;
Instantiate(reaperPrefab, spawnPosition, Quaternion.identity);
}
}