Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we destroy child objects in edit mode(Unity3d)?

Tags:

c#

unity3d

It is kind of strange things, but if you'll try to destroy child objects with DestroyImmediate function, then you will have unpredictabe result.

In my case Unity did not destroyed all childs, but did it for only half of them.

        foreach(var child in parent)
        {
            DestroyImmediate(child);
        }
like image 862
Vladislav Hromyh Avatar asked Jun 30 '16 10:06

Vladislav Hromyh


2 Answers

This is how i destroy children in Edit Mode:

for (int i = this.transform.childCount; i > 0; --i)
    DestroyImmediate(this.transform.GetChild(0).gameObject);
like image 120
Paul Delobbel Avatar answered Sep 28 '22 06:09

Paul Delobbel


As one guy consider in link it is possible with creating temp array/list.

For example:

        var tempArray = new GameObject[parent.transform.childCount];

        for(int i = 0; i < tempArray.Length; i++)
        {
            tempArray[i] = parent.transform.GetChild(i).gameObject;
        }

        foreach(var child in tempArray)
        {
            DestroyImmediate(child);
        }

Link that helped me: http://answers.unity3d.com/answers/678073/view.html

like image 35
Vladislav Hromyh Avatar answered Sep 28 '22 07:09

Vladislav Hromyh