Not Able To Populate Image[] On Start

Relatively new to both C# and Unity. The problem I’ve got below is my Image[ ] is not collecting the parent’s child Images on start. Debug tells me the Image[ ] length is 0, and Texts[ ] can’t get to the grandchildren. I’ve got all the appropriate usings as well. I could use public Images[ ] to just manually populate the list, but I want it set up to handle varied lengths eventually. Anything obvious I’m missing?

public GameObject parentObject;
private Image[ ] childItems = new Image[20];
private Text[ ] grandchildTexts = new Text[20];

void Start () {
childItems = parentObject.GetComponentsInChildren();
for (int i = 0; i < childItems.Length; i++) { godTexts = childItems*.GetComponentInChildren(); }*
Debug.Log(childItems.Length + " childlings");
Debug.Log(grandchildTexts.Length + " grandchildren");

Please read this for how to insert code nicely into the forums: Using code tags properly - Unity Engine - Unity Discussions

in your loop, you forgot to do: childItems[i].GetComponentInChildren<Text>();
As for why your first array isn’t populated, I’m not sure. Possibly because the parent object isn’t correct.

Wait, in fact there is a variable ‘godTexts’ which isn’t even declare.d What’s that? :slight_smile:

for (int i = 0; i < childItems.Length; i++)
{
      godTexts = childItems.GetComponentInChildren<Text>();
}

childItems is an array, so this will not work.
Need to do this. But as @methos5k said. What is godTexts?

for (int i = 0; i < childItems.Length; i++)
{
      godTexts = childItems[i].GetComponentInChildren<Text>();
}

Now, your image array has a length of 20. However, when you use GetComponentsInChildren call, it creates an array and tries to assign it to that variable, thus replacing it. If it returns a length of 0, that means it didn’t find any image components in the children.

Maybe show your hierarchy.

Sorry. Was in a rush and posted the wrong version. Here’s what the original code is. (tagged properly, thank you)

    public GameObject godObject;
    private Image[] godItems = new Image[20];
    private Text[] godTexts = new Text[20];

    void Start () {
        godItems = godObject.GetComponentsInChildren<Image>();
        for (int i = 0; i < godItems.Length; i++) { godTexts[i] = godItems[i].GetComponentInChildren<Text>(); }
        Debug.Log(godItems.Length + " godItems");
        Debug.Log(godTexts.Length + " godTexts");
    }

As for if the parent is wrong, I have the parent set in the editor and it’s showing the correct one. Only things under the parent are the UI image objects, and each one has a child text

So…
You have
parent <–godObject
-child1 <–child objects have image components on them
–text1 <–text grandchildren have text components on them
-child2
–text2
-child3
–text3

Are the child objects inactive?
If they are, you’ll need to set the bool to include inactive objects Unity - Scripting API: GameObject.GetComponentsInChildren

That definitely seems to be the problem. Setting the bool to true won’t help, though. It works only if I have them set active to begin with.

So maybe embarrassing. Set the bool for the one checking for texts, not the one checking for images. Been a long day :face_with_spiral_eyes: Works fine now. Thanks for all the help!

Glad you got it working! And yes, I understand. We all have those days.

Cool, glad ya got it sorted out :slight_smile: