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.

Access TextMeshPro text through script?

Discussion in 'UGUI & TextMesh Pro' started by Ch33ri0s, Apr 22, 2018.

  1. Ch33ri0s

    Ch33ri0s

    Joined:
    Feb 6, 2013
    Posts:
    54
    On a typical text UI component I would just use "Text" and make it a public variable. I just need to know how to change the text using textmesh pro? Or am I better off just using a text UI component for this?

    Thanks.
     
    tiagokb, T1M00R, Ovinsi and 1 other person like this.
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    There are two TMP components who both derive from TMP_Text. They are both in the TMPro namespace.

    The first component of type <TextMeshPro> is designed to replace the old TextMesh which uses the MeshRenderer.

    The 2nd of type <TextMeshProUGUI> is designed to replace UI.Text and designed to work with the CanvasRenderer and Canvas system.

    This information is also available in the FAQ - Question 9 on the TextMesh Pro user forum.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. /// Include the name space for TextMesh Pro
    4. using TMPro;
    5.  
    6. public class Example : MonoBehaviour
    7. {
    8.     private TMP_Text m_TextComponent;
    9.  
    10.     private void Awake()
    11.     {
    12.         // Get a reference to the text component.
    13.         // Since we are using the base class type <TMP_Text> this component could be either a <TextMeshPro> or <TextMeshProUGUI> component.
    14.         m_TextComponent = GetComponent<TMP_Text>();
    15.  
    16.         // Change the text on the text component.
    17.         m_TextComponent.text = "Some new line of text.";
    18.     }
    19. }
     
  3. Ch33ri0s

    Ch33ri0s

    Joined:
    Feb 6, 2013
    Posts:
    54
    Exactly the information I needed. Thanks!
     
    ibbitzzz, Fewpwew130 and Stephan_B like this.
  4. nadine505

    nadine505

    Joined:
    Feb 5, 2018
    Posts:
    2
    The VS editor says using the TMPro directive is unnecessary, yet the TMP_Text type could not be found. I am using Unity 2018.4.3 so maybe this is done differently in this version of Unity?
     
  5. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Nothing has changed in terms of the requirement to use "using TMPro;"
     
    YTAVATY and Fewpwew130 like this.
  6. garethdenyer

    garethdenyer

    Joined:
    Aug 16, 2020
    Posts:
    6
    This was very helpful and has allowed me to attached live text to all sorts of objects, updating during the game.

    I did need the namespace "using TMPro" but I didn't seem to need the step "private TMP_Text m_TextComponent;"

    So having declared the TMP_Text variable with the line "public TMP_Text theTMPgameobject;" I was then able to populate the text with "theTMPgameobject.text = "the text I wanted here";

    Fantastic stuff... many thanks!!
     
  7. naxs

    naxs

    Joined:
    May 14, 2021
    Posts:
    3
    Thank you very much.
     
  8. sm0k351

    sm0k351

    Joined:
    Sep 2, 2018
    Posts:
    1
    Thanks!!!
     
  9. fuzzy3d

    fuzzy3d

    Joined:
    Jun 17, 2009
    Posts:
    215
    link is broken
     
  10. Rachan

    Rachan

    Joined:
    Dec 3, 2012
    Posts:
    511
    Thanks!!!
     
  11. BillNyeGuy

    BillNyeGuy

    Joined:
    Dec 12, 2022
    Posts:
    1
    you're a lifesaver my guy