Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 DarkSlash · Feb 23, 2015 at 05:54 PM · gameobjectdestroysendmessage

Destroy all GameObjects at once

Let's say I want to destroy all enemies at once. One thing I can do is use GameObject.FindGameObjectsWithTag, then run through the returning array and Destroy each returned gameObject.

That's the better way to do it? There's something else? SendMessage has nothing to do with this?

Comment
Add comment
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

5 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by YesNoKonrad · Feb 23, 2015 at 07:33 PM

Update (2021-02-09): This answer is quite unsophisticated. It is correct only in regards to the question and should not be considered a general solution. Please read through the comment section as well.

I suppose you have an Enemy script?

To be sure to get any enemy destroyed you could declare a static generic List myEnemies.

(needs System.Generic... something along those lines, you will see it via code completion, there also is a tutorial about that in the scripting section).

Then you would implement a static function within your class which you would call for example RemoveAllEnemies

 static void RemoveAllEnemies()
 {
   foreach(GameObject go in myEnemies)
   {
     myEnemies.Remove(go); // else there will be pointer to null
     GameObject.Destroy(go);
     
   }
 }

you would call this function like this: Enemy.RemoveAllEnemies();

A static function only exists within the class, not the instantiated objects. to access the function it has to be public... i think.

I should also mention that you would need to actualy add the object to the list on creation of the object. best done in the awake() function.

Comment
Add comment · Show 6 · 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
avatar image DarkSlash · Feb 23, 2015 at 07:50 PM 0
Share

Is this solution cheaper than GameObject.Find?

avatar image Scribe · Feb 23, 2015 at 08:16 PM 0
Share

Pretty much everything is faster than finding objects at runtime with Find

avatar image pako · Feb 23, 2015 at 08:23 PM 1
Share

Yes, its cheaper, and it will become even more cheap if a pooling solution was used to avoid the dreaded Garbage Collector, which kills performance. So, ins$$anonymous$$d of using Destroy(GameObject), you use gameObject.SetActive(false) to deactivate the enemies (no garbage collection). Of course this means that enemies can be reused with gameObject.SetActive(true) ins$$anonymous$$d of spawning them.

A number of free and paid pool solutions are available in the asset store:

https://www.assetstore.unity3d.com/en/#!/search/pool

avatar image YesNoKonrad · Feb 24, 2015 at 09:33 AM 0
Share

Yeah pako +1. that would be the next step. saw other threads about that topic. i think i marked my answer as changeable from anyone... so if someone cares about changing/ improving details of the given answer or adding to it, feel free.

avatar image BergOnTheJob · Oct 16, 2020 at 12:59 PM 0
Share

The Answer is wrong.

You must go backwards through the list.


    static void RemoveAllEnemies()
    {
         for (int i = myEnemies.Count; i >= 0; i--)
         {
               GameObject go = myEnemies[ i ];
               myEnemies.Remove(go);
               GameObject.Destroy(go);
          }
    }

avatar image YesNoKonrad BergOnTheJob · Feb 09, 2021 at 08:07 PM 0
Share

The answer is not wrong. It's quite crude but it works. And though I agree that it's not a good solution, your improvement is only so much better considering you still don't use pooling, the function is static and not thread safe and all the other problems that exist with the solution that are far more important.

avatar image
1

Answer by ExtremePowers · Feb 23, 2015 at 07:39 PM

You could do something like this:

 void Start() {
     EM = new GameObject();
     EM.name = "EnemyManager";
 
     //Spawn 15 enemies and attach them to EnemyManager
     for (int i=0; i < 15; i++) {
         GameObject Enemy = GameObject.Instantiate(EnemyPrefab, Vector3.zero, Quaternion.identity;
         Enemy.transform.parent = EM.transform;
     }
 }
 
 private GameObject EM;
 public GameObject EnemyPrefab;
 public bool something;
 
 void Update() {
     if (something) {
         GameObject.Destroy(EM);
     }
 }
Comment
Add comment · Show 2 · 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
avatar image pako · Feb 23, 2015 at 08:12 PM 0
Share

Nice solution, but maybe it would be more complete if you included in your answer some details about which GameObject this script would be attached on, and also how a new Enemy$$anonymous$$anager would be created, if it's needed further down the game. Destroying all the enemies at once, in one point in the game, doesn't mean that no more enemies will be spawned further down the game ;-)

avatar image ExtremePowers · Feb 23, 2015 at 08:17 PM 0
Share

You could do this ins$$anonymous$$d:

 void SpawnEnemy() {
      if (!E$$anonymous$$) {
          E$$anonymous$$ = new GameObject();
          E$$anonymous$$.name = "Enemy$$anonymous$$anager";
      }
  
      for (int i=0; i < 15; i++) {
          GameObject Enemy = GameObject.Instantiate(EnemyPrefab, Vector3.zero, Quaternion.identity;
          Enemy.transform.parent = E$$anonymous$$.transform;
      }
 }

The script could be attached to an empty gameobject.

avatar image
1

Answer by pidgi2001 · Oct 17, 2020 at 09:53 PM

May I suggest one simple solution? Not tested though... Put all the GameObjects as children of one GameObject (say, a ‘big’ object named EnemyHerd, and every Enemy is a child of the big one.) Then, Destroy() the parent.

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
avatar image
0

Answer by siaran · Feb 23, 2015 at 05:59 PM

I'd avoid using GameObject.Find and its derivatives as much as possible and instead keep a list of enemies in some kind of manager class somewhere.

Other then that, if you want to destroy all enemies I don't think there is a better way then looping through your list of enemy gameObjects and calling Destroy on all of them.

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
avatar image
0

Answer by RedClawStudios · Feb 09, 2021 at 05:51 PM

Here is what I have used iterating backwards (@BergOnTheJob I tried yours but I think iteration is going wrong because you use count and it seems to be checked each loop and it changes). Also need to minus 1 to convert count to item number in list.

     int maxItemId = enemyList.Count-1;

     for (int i = maxItemId ; i >= 0; i--)
     {
         GameObject go = enemyList[i];
         enemyList.Remove(go);
         Destroy(go);
     }
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

25 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Problem with timed code-execution (audioplay and object destruction) 1 Answer

Trying to make a simple inventory: 0 Answers

Destruct gameobject by call from another script 1 Answer

GameObject.FindGameObjectsWithTag still finding destroyed object (C#) 1 Answer


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