Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TysonG · Mar 31, 2019 at 09:55 PM · playerdestroyenemycollider2droom

How to destroy all enemies within a certain collider?

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;
 
 /// <summary>
 /// This script is to be applied to each CamBounds collider to track (and destroy) enemies that are within the collider.
 /// </summary>
 
 
 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]);
         }
     }
 
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mchts · Apr 01, 2019 at 12:59 AM 0
Share

When do you create your enemies in the first place? And did you design the rooms or do you generate them procedurally?

avatar image TysonG mchts · Apr 01, 2019 at 01:36 AM 0
Share

There are just empty game objects positioned in each room which spawn an enemy when the player gets near the spawner (and a cooldown timer to spawn again after a certain amount of time). There is only a set number of rooms, each with BoxCollider2D attached to it.

avatar image mchts TysonG · Apr 01, 2019 at 07:33 AM 0
Share

Ok when you spawn them put them in a list at the same time. And when you destroy enemies just loop through that list.

avatar image toddisarockstar · Apr 01, 2019 at 03:48 AM 0
Share

you should really have a variable assigned to each enemy when they are spawned to tell what room they are in. you should also know what room your player is in at all times.

if your items are only spawned when your player is entering a room. your player's room variable should be static. this way all the spawned enemies can grab it in there Start functions. then when the player leaves you can destroy anything without a matching room number.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Nishchhal · Apr 01, 2019 at 03:45 AM

Its better you sort them up while you spawn them. The simplest way would be to make a parent gameobject and spawn all enemies as its child. You could use names for parent gameobject like "Room_1_Enemies", so it becomes easy for you to sort them out. You can track which room the player left and check if there is any Enemy holder gameobject for that room, if yes then Destroy.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distance destroy object 3 Answers

play sound after destroy the enemy by tag 1 Answer

change script so enemy car gets destroyed when collided with player car 0 Answers

How Do I Ignore a Collision Between an Enemy and the Player? 1 Answer

Player not getting destroyed when touched by enemies 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges