0
Answered
copenhagenjazz 1 week ago • updated by Lazlo Bonin (Lead Developer) 4 days ago 3

Hi

I am trying something seemingly simple but turns out its not. I just want to get a list of all child gameobjects so I can destroy them.

I found this http://answers.unity3d.com/questions/205391/how-to-get-list-of-child-game-objects.html but I havent really been able to transfer that solution to Bolt.

Any suggestions how to do that? Thanks in advance

+2

Manipulating lists and the such is tricky both in code and in BOLT, but heres the basics

The order is GetComponentsInChildren of Type Transform.  This will cycle through the children and find anything with a Transform Component, and return a temporary array of those components

Then we put the array into a foreach loop.  The body of the loop, the instruction that will be done over and over again for each item in the array, is Get GameObject (which grabs the gameobject each transform is attached to) and then Destroy GameObject.



If we wanted to save the array as a list to destroy/manipulate later instead, you would generate a new List, and save it as a BOLT variable, then the body of the loop would add each item to said list.

Check your scene variables to make sure it appears and populates!

Keep in mind, unity for some reason considers the Parent to be one of its own children, so make sure to compensate.  I recommend placing a blank script on all things you want to kill (Make sure to remove Start() and Update() from the script to reduce overhead).  Maybe call the script "Destroyable" or something.  Then use the script as the "Type" or component you search for, which will skip the parent and any other object you dont want to blow up.

+1

Thanks a lot for your detailed reply :) Really appreciated!