Help detecting if all objects have been destroyed

Hi there,

I have been trying to figure out how to check if all the invaders that have been generated by this script have been destroyed so it can change scene after. I haven’t been able to figure it out.

using UnityEngine;
using UnityEngine.SceneManagement;

public class Invaders : MonoBehaviour
{

    public Invader[] prefabs;       //Array of prefabs called invaders
    public int rows = 5;
    public int columns = 11;
    public float speed = 3f;
    public Vector3 direction {get; private set; } = Vector3.right;
    public Vector3 initialPosition { get; private set; }

    private void Awake()
    {
        initialPosition = transform.position;
        for (int row = 0; row < this.rows; row++)                                                    //Spacing between invaders vertically
        {
            float width  = 2.0f * (this.columns -1);                                                 //This give me the total width and height of the columns and rows based on the
            float height = 2.0f * (this.rows - 1);                                                   //spacing that is set multiplied by the row or column take away 1

            Vector3 centering = new Vector2(-width / 2, -height / 2);                                //Finding the centre of the grid pattern
            Vector3 rowPosition = new Vector3(centering.x, centering.y + (row * 2.0f), 0.0f);        //setting the row position using the centered x and y values

            for (int col = 0; col < this.columns; col++)
            {
                Invader invader = Instantiate(this.prefabs[row], this.transform);                    //spawning the Invaders from the prefabs within the row. It also stores the result of this to invader
                Vector3 position = rowPosition;
               
                position.x += col * 2.0f;                                                            //Spacing between the invaders horizontally
                invader.transform.localPosition = position;
            }
        }
       
    }
    private void Update()
    { 
       
        transform.position += direction * speed * Time.deltaTime;

        //Taking the coordinates of the viewport of the world so I can
        //check when the invaders reach the edge of the screen
        Vector3 leftEdge = Camera.main.ViewportToWorldPoint(Vector3.zero);
        Vector3 rightEdge = Camera.main.ViewportToWorldPoint(Vector3.right);

        foreach (Transform invader in this.transform)
        {
            //Checking all the invaders for being disabled
            if (!invader.gameObject.activeInHierarchy)
            {
                continue;
            }
           
            //If the aliens reach the edge of the screen call Snaking()
            if (direction == Vector3.right && invader.position.x >= rightEdge.x)
            {
                Snaking();
                break;
            }
            if (direction == Vector3.left && invader.position.x <= leftEdge.x)
            {
                Snaking();
                break;
            }

        }
    }

    private void Snaking()
    {
        direction = new Vector3(-direction.x, 0f, 0f);

        //Moving down a row
        Vector3 position = this.transform.position;
        position.y -= 1f;
        this.transform.position = position;
    }
   

}

I had to put a delay of a few seconds in my script, because I started recreating them after a nested delete function, and although the delete function was finished, Unity hadn’t finished deleting them “behind the scenes” and I got errors trying to recreate them too quickly. A delay of 1.5 seconds fixed this.

“Invaders” seems to be your class handling spawns. Make a public static variable to track your current enemy count. Increment this whenever you instantiate a new invader (either in Invaders, or the Start of Invader). When an invader dies, decrement Invaders.curInvaderCount. If the variable contains 0, there is no enemies. This is the case either before you start spawning them, or after they all have been killed.

Also, personally i think it’s confusing to have an Invader and an Invaders script. Way to close, makes it easy to introduce typos, autocompletion issues, or giving a wrong impression while reading code and skipping one character. InvaderSpawnManager would be more fitting, but that’s personal taste.

Generally be careful when using public static variables. They can easily turn your code into hard to maintain spaghetti, but for closely connected classes like an entity and its spawn handler it’s usually fine.