Unity – Accessing All Children Objects Throughout Entire Various Sized Hierarchies

September 4, 2019

Accessing Full Gameobject Hierarchy

Unity

StackOverflow – Get All children, children of children in Unity3d

Link #1

Unity Answers – Find children of object and store in an array

Link #2

For a project we were looking into applying a material color change to some more complex Unity objects/models which are made up of many children objects with many materials. To do this, I needed a way to be able to grab all the materials all throughout a gameobject’s hierarchy and change them simultaneously. This led me to think of a recursive approach, and sure enough there was already one explored that I could reference online in Link #1.

Link #1 also showed me a useful feature that I did not know about Unity. You can use a foreach loop on a transform and it will go through all of the children transforms automatically. This helped make the recursive solution much easier to read, and is just a good feature to know about in the future.