Hey
How can I keep the button position fixed to the text (even if it changes)?

Thank you in advance
Both Unityâs default GUI Text objects and the now-free TextMesh Pro GUI Text objects have a property called âpreferredWidthâ. If you use that, you can size your text-box to its appropriate size every time you update your score (use its RectTransform.sizeDelta property for resizing). Therefore the text will always fit in the text box, with no overflow.
To have the button position relative to the score text box, simply move the button object underneath the score in your hierarchy view, so that itâs a child of the score. Then, position it how you want it, and when the score resizes, your button should move with it.
Note, the first paragraph is important! Even though your text box might look okay with the text overflowing, the rest of the UI system uses the textâs RectTransformâs for positional information. You must resize the text for any children to update their positions.
Objects are parented, but I donât know what you mean by âpreferredWidthâ
Perhaps he meant âBestFitâ for Text. preferredWidth is a ContentSizeFitter or LayoutElement thing, I think
And the previous poster mentioned making the button a child of the text, this way itâs layout offset will stay up to-date. Layouts can be awesome & tricky lol
Does any of that yield some good results?
Not really, I tried both ContentSizeFitter and âBestFitâ and had no luck.
Could it be because Iâm changing the score value through script?
Okay, I have 2 suggestions for you ![]()
1 is to download the TextMeshPro package. Itâs free, new, soon to be completely integrated into Unity (not yet , though, you have to get it in the asset store). You can add sprites into your text there, easily.
Now, to the solution with the UI system.
I put a Text element on the screen. I add a content size fitter and enable horizontal preferred size (verticalâs probably good to add, also).
Then, I add the image as a child of the Text element. I set itâs anchors and pivot using one of the easy presets to the right-middle combination. I adjust the imageâs offset to about +15 in the x position.
And thatâs working for me.
Text elements have properties called preferredWidth and preferredHeight. I just looked them up in the Unity documentation and almost no detail is given on them â as methos5k mentioned, I think the intent is to use them with a LayoutElement.
But you can use them manually if thatâs easier for you. For example, I Instantiate() Text elements in my game, which I donât think the layout system supports, so I manually resize my Text elements to organize my UI around them. Hereâs an example from my code:
summaryText.rectTransform.sizeDelta = new Vector2(summaryText.rectTransform.sizeDelta.x, summaryText.preferredHeight);
As you can see, I resize my summaryText element by adjusting its RectTransformâs sizeDelta property. The vertical part of that is controlled by summaryText.preferredHeight. (Note: I actually donât use preferredWidth anywhere in my code, but I assume it performs similar functions, though I could be wrong.)
TextMeshPro elements also have a GetPreferredValues() function that will return a Vector2 of width and height.