Here I have a foreach function that outputs all the gameobject with the “enemy” tag in the hierarchy. Each “enemy” tag gameobject has a static bool candestory. How do I destroy those gameobjects in hierarchy when the bool is true?
foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject)))
{
if(obj.CompareTag("enemy")){
Debug.Log(obj.name);
list1.Add(obj);
}
public class Enemy : Character
{
public static bool shoulddestroy= false;
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player")){
shoulddestroy = true;
print("you should destory on next load"+ other.gameObject);
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player")){
print("im out"+ other.gameObject);
}
}