- Home /
How to list child game objects in the same order they appear in Editor?
Hello, I've created grid which looks like this. Grid is master transform, it has children Row, Row (1) .... each of the Rows has children Tile, Tile (1)... Now I want to save this grid into my script that is attached to Grid game object. To do this I can go through them like this:
foreach (Transform child in transform)
{
foreach (Transform child in child)
{
}
}
But this doesn't seem to be in guaranteed order. So how do I guarantee same order as they appear in Editor?
Answer by Casiell · Nov 13, 2018 at 03:32 PM
I'm pretty sure that the order should be the same with your method, but I won't argue and here is a solution.
You can do a for loop where you iterate from 0 to transform.ChildCount - 1. The you retrieve children with transform.GetChild.
You can also use your method, add those children to some lists and sort them by Transform.GetSiblingIndex. This is probably less performant, but I'll leave it to you to find out which one you prefer
Your answer

Follow this Question
Related Questions
Scaling animation issue on build. 0 Answers
Rotate transform without rotation the children 1 Answer
Return array of transform 2 Answers
Trying to Recreate Transform Child Heirarchy to a List 2 Answers
Change gameObjects parent at runtime 3 Answers