Horizontal Layout Group making a Scroll Rect's Content rect width to be negative

For my inventory system, when you open the inventory there are 2 panels, one for your inventory and one for the chest/container you are looking into. So I have a horizontal layout group to split them up evenly over the screen like this:

I use a Scroll Rect to contain the slots of the inventory, and I get the width of the Scroll Rect’s “Content” object to correctly position the slots.

Without using the Horizontal Layout Group it works fine, but when I use it,

content.GetComponent<RectTransform>().rect.width

returns -120, even though I can clearly see (while selecting it in the editor during playtime) that it has a large positive width.

How can I get the width of the Content object’s rect since it’s being stretched?

1 Answer

1

I really need to get to know the UI system better…

In the end I made 2 RectTransforms (left & right, these contain the inventory and container panels) and made them stretch to the whole screen, and set their anchor min/max’s X to 0.5, which is 1/2 of the screen always.