Problem getting parent/sibling of AutoLayout UI element [4.6]

Hi there,

I’m having some difficulty accessing the siblings of a button in a horizontal layout group.

The code works fine if the element is outside the horizontal layout group, but throws a null reference exception when it is inside.

I found this in the scripting docs, but the info is minimal:

I’m pretty sure I need to check the parent/childindex/siblings after the “PostLayout” section, but I have no clue how you might go about this.

Anyone have a clue how to achieve this? Or maybe I’m barking up the wrong tree.

Thanks!

You may want to provide your code, as we can’t tell what you’re doing wrong, but here’s a generic answer.

You can use foreach to access child objects of a transform. So your code would looks something like this (assuming it’s attached to one of the button siblings).

Transform parentTransform = transform.parent;

foreach (Transform sibling in parentObject){
    
    //Do stuff
    
    //Note that one of these is going to be yourself, you can test for that with something like:
    if (sibling == transform){
        //May I don't want to do the same stuff
    }

}

Hope that gets you started!