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
3
Question by cdutoit · Sep 02, 2013 at 05:02 PM · arraydestroyiterate

Why is iterating an array and destroying objects considered bad practice?

I am writing a "match-3" style game. My matching algorithm builds an array of objects which need to be destroyed. That is, after my match check, I iterate through the array, destroying the objects in it.

While I realize I should use Destroy over DestroyImmediate, I'm really curious about this comment in the reference manual:

Also note that you should never iterate through arrays and destroy the elements you are iterating over. This will cause serious problems (as a general programming practice, not just in Unity).

(http://docs.unity3d.com/Documentation/ScriptReference/Object.DestroyImmediate.html)

Why is this bad practice - This is exactly what I'm doing and I'd like to know what "serious problems" they are referring to. I assume an array of objects is in fact an array of references to objects, so I'm not sure why this is bad practice to delete the referenced object and what harm it will cause.

And, in light of that, how would one delete the objects which you have collected in an array if you can't iterate them?

Thanks for any insight,

Chris

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

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by SoundLogic · Sep 02, 2013 at 06:09 PM

Suppose I have an array {5,4,4,3,4,6}, and I want to remove all the 4s

 for(int i=0;i<array.length;i++)
 {
     if(array[i]==4)
          array.remove(i)
 }

now this code will go:

i=0:not a 4. Do nothing. Result: {5,4,4,3,4,6}

i=1:a 4. Remove. Result: {5,4,3,4,6}

i=2:not a 4. Do nothing. Result: {5,4,3,4,6}

i=3:a 4. Remove. Result: {5,4,3,6}

i=4:i>=array.length. End.

final result: {5,4,3,6}

Note that not all 4s have been removed. See the problem?

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 TharosTheDragon · Oct 06, 2017 at 12:48 AM 0
Share

This doesn't explain why you can't iterate through it backwards...

avatar image
2

Answer by Rawkjs · Sep 02, 2013 at 07:34 PM

This is indeed bad practice for anything that is not handled by Unity. If you were to loop in an array of GameObject or components, this would not really matter if you use the Destroy method. Here's why :

As suggested by SoundLogic above, when iterating over an array and removing objects from the array, you might end up skipping objects.

If you only use the Destroy or DestroyImmediate though, the object will be disposed of but will not be removed from your array. Which means that the iteration will got to completion. Beware though as this means that the disposed objects will stay in your array. This can be a problem if your array doesn't only serve the purpose of destroying objects.

The best way to actually remove/destroy objects from an array while iterating over it without consequences would be to do a reverse loop as such :

 for (int i=array.Count; i >= 0; i--) {
     if (array[i].NeedsDisposing) {
         Destroy(array[i]);
         array.remove(i);
     }
 }

This way, you can remove objects in a loop without getting an out of bounds exception!

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 Artifact-Jesse · Sep 18, 2016 at 05:46 AM 0
Share

So what is actually happening to the array of GameObjects in memory? Is it that Object.DestroyImmediate is blowing away the reference in the array, which the Enumerator needs in order to function properly or something?

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

20 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

Related Questions

Iterating through and deleting gameObjects 1 Answer

"For"-Loop only works for final element in array 1 Answer

How can I parent each item from one array to each item in another array? 1 Answer

Instantiate same PreFab to an array of GameObject by RayCast. 1 Answer

Can't copy Gameobject into array, only refer to an existing one. 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