Cancel Spawn without Ending Round/Wave

So I’ve been working with one of the Unity tutorials for a while and have moved on to making additional modifications/core game modules.

I have a wave spawn timer that (for wave 1) lasts/spawns enemies for 30 seconds. Currently my logic is as follows:

Has it been longer than 30 seconds? If so, stop spawning enemies and load the next scene (Wave 2)

At first I thought I could just use invoke to delay the time between stopping the spawns and loading the next level but I encountered a problem. The next round will load even if there are enemies still in the current scene (which isn’t a major problem but I would like to force the player to kill all enemies before being able to move on)

using UnityEngine;

public class EnemyManager : MonoBehaviour
{
    public PlayerHealth playerHealth;
    public GameObject enemy;
    public float spawnTime = 3f;
    public Transform[] spawnPoints;
	private float timer;
	
    void Start ()
    {
        InvokeRepeating ("Spawn", spawnTime, spawnTime);
		timer = Time.time + 30;
    }
	
    void Spawn ()
    {
		if(playerHealth.currentHealth <= 0f) 
		{
			return;
		}

		if (timer < Time.time) {
			CancelInvoke();
			Application.LoadLevel(1);
		}
			
		int spawnPointIndex = Random.Range (0, spawnPoints.Length);

        Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    }
}

What I would like to do logically is the following:

Has it been longer than 30 seconds? If so, stop spawning enemies. Are there any active enemy gameobjects in the scene? if so do nothing. If there are not then load the next wave.

How do I check to see if there are any active enemy gameobjects in the current scene?

Any help is appreciated as it doesn’t seem to be the easiest thing to solve.

The root of this question is about scene and reference management. It’s a question we get half a dozen times a day it seems. I wonder if there’s not enough emphasis on this topic in Unity lessons here.

Reference and scene management are important game design fundamentals, but the topic isn’t exactly simple. There are alternate styles and preferences, but generally the most convenient way of tracking references wins.

Sometimes the script responsible for spawning an object can keep a collection of those spawned instances. (e.g. the level is cleared if the time is up and the list is empty).

Eventually in a more complicated project, you’ll want to explore other more sophisticated ways of keeping track of what’s going on in your game.