Deleting an array of gameObjects

I need to delete an array of gameObjects

Is there a way to do that?

Array.Clear doesn’t seem to work. :frowning:

GameObject[] boxes;
boxes = GameObject.FindGameObjectsWithTag("Box");
Array.Clear(boxes, 0, boxes.Length);

Why do you think it isn't working?

1 Answer

1

Array.Clear does not actually destroy/delete the GameObjects, it just removes the reference from the array. To delete the GameObject, you should use Destroy.

GameObject [] boxes = GameObject.FindGameObjectsWithTag("Box");
for (int i = boxes.Length - 1; i >= 0; i--) {		
   Destroy (boxes[i]);
}

Thank you ArachnidAnimal!