how do i make a wall destroy itself when there is no more enemies in a area?

I’m currently trying to make a sort of barrier that prevents the player from passing it without killing the enemies in a area, my original plan was for a game object with a huge box collider2d to detect if there was anything with the Tag enemy, and if there wasn’t, it would destroy itself. I tried to use some debugs to check if the action happened, but nothing
Here is the script i made with a friend’s help:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DetectEnemy : MonoBehaviour
{
    public GameObject parede_Invisivel; // Reference to the Parede_Invisivel GameObject

    private void OnTriggerEnter2D(Collider2D other)
    {
        // Check if the entering collider is tagged as an enemy
        if (other.CompareTag("Enemy"))
        {
            Debug.Log("ainda a inimigos");
            // There's an enemy in the box collider, do nothing
            return;
        }

        // There's no enemy in the box collider, destroy the Parede_Invisivel GameObject
        Destroy(parede_Invisivel);
        Debug.Log("inimigo destroido2");
    }

    private void OnTriggerExit2D(Collider2D other)
    {
        // Check if the exiting collider is tagged as an enemy
        if (other.CompareTag("Enemy"))
        {
            // An enemy has exited the box collider, do nothing
            return;
        }

        // Check if there are still enemies in the box collider
        Collider2D[] colliders = Physics2D.OverlapBoxAll(transform.position, transform.localScale / 2, 0f);


        foreach (Collider2D collider in colliders)
        {
            if (collider.CompareTag("Enemy"))
            {
                Debug.Log("ainda a inimigos 2");
                // There's still an enemy in the box collider, do nothing
                return;
            }
        }

        // There are no enemies left in the box collider, destroy the Parede_Invisivel GameObject
        Destroy(parede_Invisivel);
        Debug.Log("inimigo destroido1");
    }

    // Visualize the box collider in the Scene view
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.yellow;
        Gizmos.DrawWireCube(transform.position, transform.localScale);
    }
}

OnTriggerExit is called when the enemy exists the collider. I’m not sure it gets called when you disable the enemy. Also the player might still be inside the collider when it goes toward the wall you mentioned.

In order to find out if there is any enemy left, i suggest having a Singleton class such as EnemiesManager that controlles enemies spawn and death. Here you can have a counter that keeps the track of alive enemies so you can call this class when any enemy gets killed by the player and using that counter, you can find out if there is any enemies left or not and if not, disable the wall in the scene using that class.

Also this approach about a great collider and wall, doesn’t seem good to me. In your game, you probably have some walls that you want to still have them even after the player kills all the enemies. but you also have some “Doors” that player should cross to go to the next area. You can have these Door gameObjects and add collider to them disable them after all th enemies are gone.

Using a list of Doors gives you the ability to have multiple doors in the scene as well as the walls that will never be disabled. Later on you can add some kind of animation to disable the doors. Also you con’t need a huge collider anymore.

When an object is destroyed it doesn’t trigger OnTriggerExit.

Instead you can use Invoke or a coroutine to check for enemies within an area once per second.

And you may find it easier to just do FindWithTag(“Enemy”) instead of Physics.OverlapBox.

Just to note that I appreciate this is discussin 3D but wanted to mention that it does in 2D but there’s also an option to control it: https://docs.unity3d.com/ScriptReference/Physics2D-callbacksOnDisable.html

What I don’t understand is that you said a BoxCollider2D yet posted script showing interaction with 3D physics.

oh i didn’t notice that, thanks for pointing it out that might be one of the reasons it wasn’t working.