Is there is any way to destroy all the children of an object? I would like it in C# if possible.
Something along these lines:
Transform parent;
foreach(Transform child in parent) {
Destroy(child);
}
Hm it doesn’t help too much
That code doesn’t work. First of all, you can’t destroy a Transform; you would need child.gameObject. But even with that, it won’t work. Shawn White from UT gave me a good explanation, when I didn’t understand what was happening:
Here’s one way to do it:
var children = new List<GameObject>();
foreach (Transform child in transform) children.Add(child.gameObject);
children.ForEach(child => Destroy(child));
That looks great, I got the explanation, but can you put it in C# please
He’s basically creating a new collection, then iterating through that collection. It’s pretty basic programming in itself, so I would recommend reading up on C# Lists (and other collections) as you’re going to have to learn to love them for game programming.
So children var should be a List type var?
That is C#.
–Eric
The variable declaration is done in JS way so I thought it all is.
I used var instead of List because the latter makes the code look redundant to me. Also, because I don’t have an autocompleting IDE, I don’t want to type it, either.
I’m working on a 3D tile map editor, and I group my platforms in separated GameObjects. What I want to do is to make a delete all button so I can start all over with a new map, and when pressing a button, the objects in those GameObjects to be destroyed.
The variable declaration was done in a C# way, actually (since 3.0). Also JS uses for/in rather than foreach, and I don’t think it has lambda expressions.
–Eric
var children = List.<GameObject>();
for (var child : Transform in transform) children.Add(child.gameObject);
children.ForEach(function(child) Destroy(child));
Using the ForEach doesn’t look any better to me in JS:
children.ForEach(function(child) Destroy(child));
for (var child in children) Destroy(child);
but I usually prefer it in C#, though I don’t really care for this simple example (var would make it shorter, but if I’m going to write a loop, I’ll take the extra clarity of “GameObject”):
children.ForEach(child => Destroy(child));
foreach (GameObject child in children) Destroy(child);
I haven’t even heard of lambdas, it’s good I did now
What about a way without allocating the memory for all the transforms?
int childs = transform,.childCount;
for (int i = childs - 1; i > 0; i--)
{
GameObject.Destroy(transform.GetChild(i).gameObject);
}
what about - C#
while(parent.transform.GetChildCount()>0){
GameObject.Destroy(parent.transform.GetChild (0));
}
Hehe, it’s a shame i didn’t think it myself
I doubt this will work.
According to the reference manual, actual object destruction is always delayed until after the current Update loop.
Your for condition is based on the assumption that GetChildCount() is decreased as soon as Destroy() is called.
It’s more likely, though, that this happens later during the actual object destruction.
Thus your for condition will never be false, and you are caught in an endless loop.
Your post is based on the assumption it doesn’t
My post is based on the assumption that this thread is about finding a solid solution for destroying all children objects :).
Using code that relies on implementation details of the framework doesn’t sound like a good idea to me.
LBandy’s code snippet looks like a solid solution (although you would have to replace i>0 with i>=0 in the for condition, otherwise you’re missing out on the first child node).