Clearing a GridLayoutGroup...

I have a GridLayoutGroup object. In a script, I add icons (represented as RawImage objects) to it as children at runtime. However, I sometimes need to remove all of them. I tried iterating through them and calling Destroy() on each of them, but I found that if I insert icons again, they continue to be placed in slots to the right of or below the destroyed objects.

What is the proper way to clear all items from a GridLayoutGroup in a script? I cannot find any type of clear method.

I know that I probably could also call Destroy() on the GridLayoutGroup object and recreate it from a prefab, but that is a very big hack.

Hi,
you are doing something wrong. I do a same thing as you, and it’s working for me. When you destroy the gameObject, you have to destroy the “base gameObject”, first child of parent with GridLayoutGroup. If you destroy some child of this object(what are you probably doing), the “base game object” will stay as child of parent with gridLayoutGroup.

Try adding a “Content Size Fitter” component on your GridLayoutGroup. Then should work “iterating through them and calling Destroy()”
that should be something like this:

GameObject[] GOS = GameObject.FindGameObjectsWithTag ("tag");
    		foreach (GameObject go in GOS) {
    			Destroy (go);
    }