0

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
.GetComponentsInChildren<Transform>();
.transform.GetChild(i);
.transform.childCount;

In image 2:

//Will see and return nothing
.GetComponentsInChildren<Transform>();
.transform.GetChild(i);
//Will see and return both inactive children
.transform.childCount;

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?

EDIT:

List<int> pos = new List<int>();
int runningCount = 0;

if (win.lineNumber != -1) 
{
    pos = slot.lines[win.lineNumber].positions;
}

for (int reel = 0; reel < slot.reels.Count; reel++) 
{
    for (int i = 0; i < slot.reels[0].symbols.Count - (slot.reelIndent * 2); i++) 
    {
        var color = new Color32();

        if (win.setIndex != 10) 
        {
            if (win.matches > runningCount && pos[reel] == slot.reelIndent + i) 
            {
                runningCount++;
                color = new Color32(255, 255, 255, 255);
            }
            else
            {
                color = new Color32(77, 69, 69, 255);
            }
        }
        else
        {
            if (win.matches > runningCount && slot.reels[reel].symbols[slot.reelIndent + i].GetComponent<SlotSymbol>().symbolIndex == 10) 
            {
                runningCount++;
                color = new Color32(255, 255, 255, 255);
            }
            else
            {
                color = new Color32(77, 69, 69, 255);
            }
        }

        if (slot.reels[reel].symbols[slot.reelIndent + i].GetComponent<SpriteRenderer>())
            slot.reels[reel].symbols[slot.reelIndent + i].GetComponent<SpriteRenderer>().color = (color);
        if (slot.reels[reel].symbols[slot.reelIndent + i].GetComponent<Renderer>())
            slot.reels[reel].symbols[slot.reelIndent + i].GetComponent<Renderer>().material.SetColor("_EmissionColor", color);
        if (slot.reels[reel].symbols[slot.reelIndent + i].GetComponent<SlotSymbol>().symbolIndex == 9) 
        {
            slot.log("namee: "+ slot.reels[reel].symbols[slot.reelIndent + i].transform.childCount);

            Transform[] transforms = slot.reels[reel].symbols[slot.reelIndent + i].GetComponentsInChildren<Transform>();
            // slot.log("nameeee: "+slot.reels[reel].symbols[slot.reelIndent + i].transform.GetChild(0).name);
            foreach(Transform t in transforms)
            {
                slot.log("nameee: "+t);

                if (t.gameObject.GetComponent<SpriteRenderer>())
                        t.gameObject.GetComponent<SpriteRenderer>().color = (color);
            }
        }
    }
}

For further clarification, if I had a hierarchy window open showing all child objects of the parent in question named 1,2,3,4,etc..., when I try to access the children through any of the above methods, all odd numbered children will be treated as if they do not exist. As far as I can tell, all those commands can only see the even numbered children. AND, if I were to move or delete the first child from the hiearchy window, "1", it would now be flipped, I can only access all odd children. *It does not matter if the objects are marked as active or inactive when it comes to trying to access them. Changing object "1" to inactive while in the first position will not effect anything when trying to access them from the script, it will only stop them from being active in the game window (As you would expect).

(Also it should be noted that as far as rendering these objects in the game window when playing, there are no issues. Everything displays properly and exists, only accessing them is an issue).

11
  • As for GetComponentsInChildren<Transform>() will return all active children including the parent itself! If you wanted inactive ones you need to pass GetComponentsInChildren<Transform>(true) .. that is by design and API note that it will of course also bubble further down the hierarchy
    – derHugo
    Sep 19, 2022 at 11:44
  • And I don't quite understand your tests ... GetChild(i) should return the child at given index no matter if it is enabled or not
    – derHugo
    Sep 19, 2022 at 11:45
  • If you only want to iterate all direct children you can use foreach(Transform child in parentTransform){ ...}
    – derHugo
    Sep 19, 2022 at 11:50
  • The inactive children are only there to make the code work, without them (with only the 2 active children) any of the above commands will only see and/or return the second object. If I were to switch their order in the hierarchy it would still only see/return the second child. As for .GetChild(i), I may have been incorrect about it not seeing the inactive ones, that's my bad. But that's not necessary for what I'm doing. Sep 19, 2022 at 11:53
  • foreach through the parentTransform will not work as intended. It will only check the children in the even positions. So if I laid my hierarchy out like image 2 I would not see either active chidren, and if I delted both of the inactive children I'm not using, I would only loop through 1 of the objects (Whichever is in the second position). *This isn't what I want to do, this is what's happening Sep 19, 2022 at 11:56

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.