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
8
Question by Piesk · Apr 01, 2014 at 12:02 PM · destroyimmediate

destroyImmediate on children not working correctly?

So far I have managed to get around this problem by doing other methods instead, but it seems than when running a foreach loop like so:

 foreach(Transform child in star.transform){
                     DestroyImmediate(child.gameObject);
                 }

It will destroy some but not all, this time around it leaves 1 alive, last time i had this problem with something else it left 3. I THINK the ones left alive were gameobjects that had been created before runtime, then been through a cycle at runtime.

For example: i create the object, press play, press stop. Now if i run my method to destroy all child transforms that object is no longer destroyed. IE it seems to no longer be considered inside "star.transform". What am i missing here?

Many thanks!

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

6 Replies

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

Answer by Bunny83 · Apr 01, 2014 at 12:11 PM

This answer doesn't exactly answer your problem but it's actually the same issue. You shouldn't use DestroyImmediate anyways. It's mainly for use inside the UnityEditor like you can read in the docs.

So the usual way is to simply use Destroy instead of DestroyImmediate. Destroy is only delayed until the end of the current frame.

edit
So from your comment it seems like you are actually writing an editor script. In this case you have to use a temporary array / list and iterate over this. The IEnumerator of Transform uses childCount and GetChild to iterate through the childs. So modifying the collection while you iterate through them won't work. Use this instead:

 var tempList = transform.Cast<Transform>().ToList();
 foreach(var child in tempList)
 {
     DestroyImmediate(child.gameObject);
 }

Don't forget to add the "System.Linq" namespace.

Comment
Add comment · Show 5 · 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 Piesk · Apr 01, 2014 at 12:14 PM 0
Share

I am using it in unity editor^

avatar image Bunny83 · Apr 01, 2014 at 12:43 PM 0
Share

Sorry, that wasn't clear from your question, especially because you talked about runtime. Runtime usually referes to when the game runs.

You should make it more clear when you talk about code that isn't inside a $$anonymous$$onoBehaviour because that's the usual case.

avatar image Piesk · Apr 01, 2014 at 02:34 PM 0
Share

$$anonymous$$y apologies if it wasn't clear to you, but i actually said that AFTER runtime the problem occured. Explaining that it happens after you stop the game. But never$$anonymous$$d, this is essentially what i did with the last problem. Thanks.

avatar image LexLife · Dec 21, 2018 at 10:57 AM 0
Share

Thanks for your help!

avatar image kahlerasse · Jul 28, 2021 at 08:45 PM 0
Share

thanks a lot man !

avatar image
15

Answer by msnyder102 · Aug 09, 2014 at 08:08 PM

I ran into a similar problem using this worked for me

 while(transform.childCount != 0){
             DestroyImmediate(transform.GetChild(0).gameObject);
         }
Comment
Add comment · Show 3 · 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 rutter · Aug 09, 2014 at 08:08 PM 0
Share

I like this solution; in my tests, it's both faster and less memory intensive than the usual "allocate a temp array" thing people seem to like doing.

avatar image Thompsan · Oct 18, 2014 at 11:14 PM 2
Share

I use for loops ins$$anonymous$$d to avoid getting stuck in infinite loops ;)

 for(int i = transform.childCount - 1; i >= 0; i--)
 {
      DestroyImmediate(transform.GetChild(i).gameObject);
 }

avatar image RyanNguyen · Oct 21, 2017 at 04:04 PM 0
Share

Thanks you, work like a charm.

avatar image
1

Answer by DerDicke · Jul 06, 2017 at 03:11 PM

Piesk:

Seems to work for me when deparenting the obj first in Unity 5.5: oldTile.transform.SetParent(null); GameObject.DestroyImmediate(oldTile);
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 bluksPL · Jan 24, 2017 at 12:05 PM

Similar (and simpler) soultion, but without alocateing new List. Also iterate from last to first sibling (do not require relocate a Array of Transforms):

 foreach(var child in transform.Cast<Transform>().Reverse())
             DestroyImmediate(child.gameObject);

Also: "Don't forget to add the "System.Linq" namespace."

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 Hellium · Jan 24, 2017 at 12:10 PM 0
Share

Using Linq is not recommended at all in Unity application and produces garbage which is more work for our friend the GC (Garbage Collector)

avatar image
0

Answer by Giacas · Feb 27, 2015 at 01:57 PM

I use this this for loop to destroy objects inside a transform (childs will change dynamically).

 Transform[] tArray = transform.GetComponentsInChildren<Transform>(false);
 int cCount = tArray.Length;
 string origName = "prefabOggetto";
 
 for (int i = 0; i < cCount; i++)
 {
     if (tArray[i] == null)
     {
         continue;
     }
     Debug.Log("Child name: " + tArray[i].name);
     GameObject go = tArray[i].gameObject;
     if (go.name == string.Format("{0}(Clone)", origName))
     {
         Debug.LogWarning(" Destroy: " + go.name);
         DestroyImmediate(go.gameObject, false);
     }
 }

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
  • 1
  • 2
  • ›

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

31 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

Related Questions

Using DestroyImmediate on one GameObject makes a reference to another a null 1 Answer

Instantiate object inside outher game object and destroy object 0 Answers

Removing only colliders in a game object and its children 1 Answer

(Editor) How to delete duplicate components from GameObject that persists when the scene reloads 0 Answers

Component Find and Replace: execute's fine, fails to do anything?! 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