r/ProgrammerTIL • u/NachoAverageCabbage • Jul 28 '19
Unity [Unity] You can foreach through a Transform to enumerate its children
Today, I noticed Transforms don't have a GetAllChildren() method, so I googled how to do this. I stumbled upon this thread, and apparently Transform implements IEnumerable, allowing you to foreach (enumerate) through its children.
I don't know how I feel about this... I guess it could be useful, but IMO Transforms shouldn't be enumerable, as they are much more than a collection of children.
45
Upvotes
4
u/nicmarxp Jul 28 '19
Sounds cool, how about someone making a performance test compres to GetAllChildren?:) i’d do it but I’m in the forest.. 😛
2
u/get_salled Jul 29 '19
So you agree they are a collection of children?
5
u/cheeseless Jul 29 '19
I'd say, IMO, that being enumerable is appropriate for any object that has a well understood "main" collection inside it.
3
u/ForkForkFork Jul 29 '19
That’s a nice tip!
Minor pedantic notes: to enumerate is to name things. As in this case, read them of a list.
Iteration is the process of repeated cyclical events.
The Transform is enumerating it’s children for you to iterate through.