Getting the width of UI text inside a layout allways 0.

Hello,
I simply want to get the width of an element inside a horizontal layout at runtime. Currently I always get 0.0, using RectTransform.rect or RectTransform.sizeDelta. The width is definitely not 0.

Any ideas what the problem is? Or how to bypass it to find out the width?

I just tested this and I guess you’re getting the size on the Awake or Start method, but you have to wait one frame before getting the correct sizeDelta or rect of the elements. I guess the reason for this is that the HorizontalLayoutGroup component adjusts the sizes on the first frame after all the Start methods have been called.

Instead of the common “void Start()” method, use “IEnumerator Start()” method. It’ll work just like the first one, but it’s now a coroutine so you can yield for one frame. So, instead of this:

void Start() {
    Debug.Log(GetComponent<RectTransform>().rect.width);
}

which prints “0”, use this:

IEnumerator Start() {
    yield return null;
    Debug.Log(GetComponent<RectTransform>().rect.width);
}

why is this still a thing? this is so annoying. also was working perfectly for me first few plays. then out of nowhere, bam. busted. dumb