I have one probably stupid question, but cant find anything useful in unity documentation.
I have a 2d object that is a panel, and button within it, as I wanted to have 5 buttons with same height that would fill that panel, my idea was to get height of panel and then get 20% of it.
But what I am not getting is how this height thing even works, I have panel of 500 height, and button of 500 height that are not same size?? Can’t find anything useful on this topic so bumping here for tips.
I am talking about rect transform height.
pictures for the reference


This is UGUI not 2D. I’ll move your post to the UI forum.
1 Like
One you fundamental part of how unity works that you need understand is that all the positioning, rotation and scaling is based on object hierarchy. This is somewhat described in the “Parenting” section of documentation for transform Unity - Manual: Transforms . While RectTransforms aren’t exactly the same as regular Transform they share a lot of stuff and most of it applies equally to both.
For example if you have object hierachy like this:
* box 1 (scale= 0.3)
* box2 (scale = 0.25)
* box4 (scale=2, height=100)
* box3 (scale = 0.5)
* box5 (scale=2, height=100)
Even though both box4 and box5 have the same local height and scale, their actual height on screen will be different. The height of box4 will be 0.30.252100=15, but box5 will be 0.30.52100=30.
The difference you are observing is most likely due to one of the ancestors (parents, or parent parent …) have different scale.
As a general advice strongly recommend you to never set the scale to anything else than 1,1,1 for GUI objects (the ones with RectTransform which are inside canvas). That way it will be a lot more easier to deal with dimensions and they will be directly comparable between multiple objects.
Even if you didn’t intentially change the scale you might have done it accidentally so keep an eye for it. There two tools for changing size : Scale tool and Rect tool. Both will seemingly cause the size of object change, but for RectTransforms you want to use the second. Scale tool will change the scale but Rect tool changes width/height.
You can also get weird scale if you created a GUI object by dragging a prefab into scene without parenting to canvas and afterwards reparented the new object somewhere under the canvas.
1 Like
Thanks a lot man, helped a ton!