Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Socapex · Jun 06, 2015 at 09:31 PM · uirecttransformwidth

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by DiegoSLTS · Jun 07, 2015 at 01:05 AM

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);
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Socapex · Jun 07, 2015 at 06:21 AM 0
Share

Awesome! I ended up extracting my objects in a different empty, which ended up working. But your answer is very clear and clever :) I thought it may have been the Start(), but I wasn't aware of a way to call anything right after Start. This will definitely come in useful in other situations, thank you!

avatar image dappledore · Aug 26, 2016 at 05:50 AM 0
Share

genius answer

avatar image Kadjai · Jun 10, 2020 at 08:22 PM 0
Share

It works. $$anonymous$$y first 4 times finding width worked fine without this, but the 5th... nah. But they all work with the IEnumerator adjustment.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Incorrect RectTransform width value when trying to read child RectTransform width. 0 Answers

RectTransform Pivot Point in the new UI? 1 Answer

Force Unity UI element to refresh/update? 4 Answers

How to make a health bar in Unity 5? 0 Answers

Change RectTransform pivot without changing position 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges