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
2
Question by Gumpert · Feb 20, 2014 at 05:45 PM · arraydestroyarray of gameobjects

How do I destroy all the GameObjects in an array?

Hi, I'd just like to know if there's some quick way of destroying every GameObject in an array of them. I tried Destroy(array), but Unity didn't like it. I could destroy each one individually using a loop or something, but I think there must be an easier way of doing it than that... Very thank you, and merry unitying!

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

3 Replies

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

Answer by clunk47 · Feb 21, 2014 at 09:09 PM

You can do a loop in a couple different ways. for or foreach will work.

 if(Inventory.Length > 0)
 {
     foreach(GameObject go in Inventory)
     {
         Destroy(go);
     }
 }

 
 
 //OR
 
 
 for(int i = 0; i < Inventory.Length; i++)
 {
      Destroy(Inventory[i].gameObject);
 }
Comment
Add comment · Show 7 · 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 jister · Jan 11, 2016 at 12:16 AM 3
Share

you could risk getting the error that your are changing the size of your iteration while iterating this way if I'm not wrong. I thought it was better to iterate backwards in case of deleting elements. (do correct me if I'm wrong)

 for(int i = Inventory.Length-1; i >=0; i--)
  {
       Destroy(Inventory[i].gameObject);
  }
avatar image JoshuaMcKenzie jister · Jan 11, 2016 at 06:58 AM 0
Share

you shouldn't have to worry about that in a loop. Destroy only flags the object to be deleted later, it doesn't do it immediately.

avatar image jister JoshuaMcKenzie · Jan 11, 2016 at 12:54 PM 0
Share

According to this it should. I know I had it several times when removing in a forward loop.

avatar image Immanuel-Scholz jister · Jan 11, 2016 at 02:04 PM 1
Share

Iterating backwards is only a solution if destroying an item from "Inventory" will change the "Inventory" collection by removing that very item that got destroyed, (which is not possible for an array anyway). It has nothing to do with using Destroy or DestroyImmediate. Also, you can still get problems, e.g. if you remove other items in an collection while destroying items from that collection.

Also, better do not get used to "foreach" for now. Although it looks nicer, due to a bug in $$anonymous$$ono, it will always collect some small memory (at least if you target systems that use the $$anonymous$$ono runtime). Best options is the simple for-loop.

avatar image jister Immanuel-Scholz · Jan 11, 2016 at 03:20 PM 0
Share

ok thanks for clearing that up ;-)

avatar image hottabych jister · Jul 06, 2018 at 01:00 PM 0
Share

This is true for Foreach loop, if you delete elements from the List using Remove method.

avatar image hottabych · Jul 06, 2018 at 01:02 PM 0
Share

First of all, use List ins$$anonymous$$d of array if adding/deleting expected. You forgot to delete elements from the inventory. If you just Destroy gameobjects, Inventory entry will still exist, but it will be null ins$$anonymous$$d of pointing to gameobject. So Inventory.Count still will be greater than zero. You should either Remove/RemoveAt elements or call Clear().

avatar image
1

Answer by jimmycrazyskills · Feb 20, 2014 at 07:12 PM

Sorry if this is wrong , but I think you can do this by using a For loop , the code below is in Javascript and uses an array called "Inventory".

 for (var i = 0; i < Inventory.Length; i++) {
     Destroy(Inventory[i]);
 }
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
-1

Answer by Contato · Apr 02, 2017 at 10:07 AM

Here it is:

 while (Inventory.Length > 0) {
      Destroy(Inventory[0]);
  }
Comment
Add comment · Show 1 · 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 hottabych · Jul 06, 2018 at 12:55 PM 0
Share

It will cause the endless cycle, because "Destroy only flags the object to be deleted later, it doesn't do it immediately" as fairly mentioned above.

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

Dynamic Array Generation via Inspector 1 Answer

Storing tag strings in an array, from an already existing array of gameobjects with different tags. 1 Answer

Moving an array with game object attached 1 Answer

Can't copy Gameobject into array, only refer to an existing one. 1 Answer

Using variables inside a GameObject[] array 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