- Home /
transform.childCount == 0, but a child shows up in the hierarchy. How is this possible?
I got an error when trying to call transform.GetChild(0). As I debugged the problem, I found out that transform.childCount was 0, so the call was out of bounds. However, I paused the game as soon as I detected that childCount was zero, and under the hierarchy it showed that the object did have a child, as shown below:
Is there any way this can happen?
In case it helps, this is the C# code snippet I used to debug the program:
if (transform.childCount == 0)
{
Debug.Log(transform.GetInstanceID());
UnityEditor.EditorApplication.isPaused = true;
return;
}
I used the instance ID to verify that the transform of the GameObject highlighted in the hierarchy was the one with childCount == 0.
This makes no sense... please help! Thanks!
I've never used HideFlags, so unless it's built in, no.
Answer by jrat · Dec 26, 2015 at 04:45 PM
I finally figured it out. I was instantiating the WaitBlock, assuming it would create its children in its Start method immediately after. However, I called GetChild before Start was called. What is still confusing is that even though I paused the game immediately, Start was still called afterward. Anyway, it's working now. Thanks for everyone's suggestions!
Answer by hexagonius · Sep 19, 2015 at 09:42 PM
by their name it's say they're runtime instantiations so they didn't come in at the same time, right? possible you're asking for child count before it exists?
No, I am 100% sure the child is instantiated and assigned its parent at the start of the scene, and then childCount is requested in a helper method called Initialize, called at the very end of the Start method.
this helper method is in a script on WaitBlock(Clone) ?
No, sorry for the confusion. I have an empty GameObject managing the scene. Both the Start and Initialize methods I referred to are called in the managing script. Initialize then calls a script a attached to the parent, WaitBlock(Clone), which asks for it's childCount.
Your answer
Follow this Question
Related Questions
Why do we access children through an object's transform? 2 Answers
Getting a tag from a child 1 Answer
Switch for children not working 1 Answer
transform.Find always returns null 1 Answer
First child of a gameobject 3 Answers