Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

How to destroy all enemies within a certain collider?

$
0
0
Hey everyone, I'm remaking a level from Mega Man 2, and when I leave a room I want all the enemies still alive in that room to be destroyed. I have a BoxCollider2D covering each room with this EnemyTracker script attached, which when the player exits this collider, every enemy is destroyed. That's the problem though, every single enemy on the map is destroyed instead of only the enemies in that room (Collider2D). Anyone know what I can do to fix this? Any help is appreciated! using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// This script is to be applied to each CamBounds collider to track (and destroy) enemies that are within the collider. /// public class EnemyTracker : MonoBehaviour { private GameObject[] aliveEnemies; private void OnTriggerExit2D(Collider2D other) { if (other.gameObject.tag == "Player") { DestroyAllEnemies(); } } void DestroyAllEnemies() // destroy all alive enemies once you leave an area (so they don't follow you) { aliveEnemies = GameObject.FindGameObjectsWithTag("Enemy"); for (var i = 0; i < aliveEnemies.Length; i++) { Destroy(aliveEnemies[i]); } } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>