,How do I detect if there are no more enemies of a type in an area?
Helloo, I have a small room filled with a few enemies. There are doors on either side of the room, they're supposed to close when the player enters the room and there are still enemies inside, then the doors should reopen when there are no more enemies in the room.
I currently have a large box collider that covers the whole room and have an OnTriggerStay script that detects whether there are any enemies left in the room. Problem is, since I'm using tags to identify them, what I think is happening is that the game is identifying 1 random enemy from the whole scene with the "hazard" (enemy) tag. This means if the identified enemy is outside of the combat room, the player just gets locked in permanently. My code is extremely messy at the moment, I've been awake way too long.
public class CombatRoom : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerStay(Collider Hazard)
{
if (Hazard.gameObject.tag == "Hazard")
{
GameObject.Find("CombatDoor").GetComponent<CombatDoor>().EnemyState = 1;
Debug.Log("enemy in");
}
if (Hazard.gameObject.tag != "Hazard")
{
GameObject.Find("CombatDoor").GetComponent<CombatDoor>().EnemyState = 0;
}
}
}
Also since this way of doing it is so broken I had to detect the player colliding with the same collision box on a different script. I imagine there's a much better way to do this, maybe 'foreach' loops? But I've never used them. Please help!
Answer by wes4ray · May 09 at 09:44 PM
probably need a list and the enemies need to have a script that checks if its spawned and the area it was spawned, then add that to a list, then check that area lists for what ever you want. i use visual scripting/Bolt so i focus more of what needs to happen then how to "Code It" but hope that helps.