Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Text.PrefferedHeight not working correctly?

Discussion in 'Scripting' started by RPGFabi, Apr 2, 2020.

  1. RPGFabi

    RPGFabi

    Joined:
    Oct 24, 2019
    Posts:
    29
    Hey,

    for my Quiz App I Change the Question and then wait one Frame and then check the prefferred Height then set the Size of the Text to the Height.

    As far so good, this works... mostly.

    I have some questions, where this does not work => I get a wrong Height, so my whole design gets pushed up and I can't read the last line.

    upload_2020-4-2_11-23-14.png
    This is where it doesnt work.

    upload_2020-4-2_11-24-33.png
    Here it does work.


    I tried it with:
    Text.preferredHeight
    and
    Text.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, Frage_Height);

    Content Size Fitter and
    Frage.rectTransform.rect.size.y


    And yes, between setting the Text and Getting the Height I wait via
    yield return null

    And like I said, it mostly works.

    The Questions are in German, but that doesn't matter sinze the Quiz will be German only.
     
  2. RPGFabi

    RPGFabi

    Joined:
    Oct 24, 2019
    Posts:
    29
    No ideas?
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    Is this a Unity Text component or a TextMeshPro one?
     
  4. RPGFabi

    RPGFabi

    Joined:
    Oct 24, 2019
    Posts:
    29
    Its Unity Text.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    Right. I'd swap to TextMeshPro - it looks better, and preferredHeight gives the correct value right after you set the text.

    If you want it to work with Unity Text, the way to fit some contained text is to have the text set to stretch with it's container, and have the container use a content size fitter.
     
  6. RPGFabi

    RPGFabi

    Joined:
    Oct 24, 2019
    Posts:
    29
    well, Even the Content SizeFitter does not work there...

    Thats my Problem. Else i wouldnt ask for help

    Ok, retested it with Content Size Fitter.

    In this case the Returned Value by Text.rectTransform.rect.size.y is not the same as it shows in the Editor