• 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 /
This question was closed Feb 10, 2015 at 01:32 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by JoeW97 · Nov 12, 2013 at 01:57 PM · c#editordestroyarrays

Using DestroyImmediate on Arrays?

The docs say this about destroying objects in an array using DestroyImmediate:

"Also note that you should never iterate through arrays and destroy the elements you are iterating over. This will cause serious problems..."

Surely they mean a dynamic array? What's the problem with doing it on a static array? I understand on a dynamically allocated array that the contents may be shuffled/removed, but I thought it was safe on a static array.

I'm writing an editor script (C#) for which I need to Instantiate clones of a GameObject and store them in this static array:

 private GameObject[] cloneSteps = new GameObject[12];

Further on I need to destroy them before re-instantiating them, so I do iterate over them:

 if(cloneSteps != null){
     for(int i=0; i<12; i++)
     {
         if( cloneSteps[i] != null)
               DestroyImmediate(cloneSteps[i]); 
     }
 }


I have been getting an intermittent bug, so I'm wondering if this is faulty? How else can you destroy/remove/delete objects in an array?

Am I right in thinking that there will be a memory leak if I simply overwrite the array before destroying the elements?

Thanks

Steve

Comment
Add comment · Show 3
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 jacobschellenberg · Nov 12, 2013 at 04:16 PM 0
Share

Iterate through them backwards. Perhaps this example can give you a start, since I'm not sure exactly what you'll need. The For loop is the most important part here.

 public void ClearChildren(GameObject parent)
 {
  List<Transform> children = new List<Transform>;
             
  foreach(Transform child in parent.transform)
  {
   children.Add(child);
  }
             
  for(int i = children.Count - 1; i >= 0; i--)
  {
   Destroy(children[i].gameObject);
  }
 }


I updated the method to be a little more generic.

avatar image JoeW97 · Nov 12, 2013 at 05:12 PM 0
Share

Thanks, definitely worth an experiment.

avatar image jacobschellenberg · Nov 12, 2013 at 05:13 PM 0
Share

Yeah it's odd and sometimes still confuses me, but from my research, this happens to be the answer. I use this exact method quite frequently.

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Nov 12, 2013 at 05:08 PM

Unity uses a heap with garbage collection as the memory model.

Am I right in thinking that there will be a memory leak if I simply overwrite the array before destroying the elements?

No. You can simply overwrite the reference to some object. Assuming there are no other reference to it, the memory will be reclaimed the next time the memory manager. A couple of links:

http://docs.unity3d.com/Documentation/Manual/UnderstandingAutomaticMemoryManagement.html

http://answers.unity3d.com/questions/7853/garbage-collection.html

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 JoeW97 · Nov 13, 2013 at 01:12 AM 0
Share

Thanks for the links. Would upvote but don't have enough rep.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Multiple Cars not working 1 Answer

Initialising List array for use in a custom Editor 1 Answer

Distribute terrain in zones 3 Answers

reflection propertyinfo.getvalue compiles fine but gives erros in editor 1 Answer

Manually creating Editor sometimes leads to NullReferenceException in SerializedObject constructor 0 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