Unity only sees child objects placed in even positions of the hierarchy window when accessing from script


This Content is from Stack Overflow. Question asked by Jakob Flannery

So I’ve got 2 children for an object, that when I try to access through any transform-child-get method, I will get an unexpected response.

So here’s an object order that works in the hierarchy window:
Objects of interest in even positions

And here’s an order that does not work:
Objects of interest in odd positions

The inactive objects are empty objects containing a transform component only. The active child are objects containing a transform and a spriterenderer component to create a 2D backdrop.
The parent contains multiple components to create a 3D animation.
Parent components

For some reason trying to access the children of this object dynamically, through any transform-child-get command I can find only looks through the child in the even positions in the hierarchy window.

For example, in image 1:

//Will see and return both active children

In image 2:

//Will see and return nothing
//Will see and return both inactive children

I cannot for the life of me work it out.
Unity Editor 2021.3.9f1, also tried in 10f1.
I have not tried remaking the project from scratch as it’s quite large already and was hoping to find a solution first.

Any other ways to access children from a gameobject dynamically I can use to access their components?


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?