- Home /
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!
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.
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.
$$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.
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);
}
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.
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);
}
Answer by DerDicke · Jul 06, 2017 at 03:11 PM
Seems to work for me when deparenting the obj first in Unity 5.5:Piesk:
oldTile.transform.SetParent(null); GameObject.DestroyImmediate(oldTile);
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."
Using Linq is not recommended at all in Unity application and produces garbage which is more work for our friend the GC (Garbage Collector)
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);
}
}
Your answer
Follow this Question
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