Search Unity

How do you add a TextMeshPro text object to a script?

Discussion in 'UGUI & TextMesh Pro' started by DustyShinigami, Jan 8, 2019.

  1. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    I've read that you have to use the GetComponent function and have TextMeshPro as the type. Eg: GetComponent<TextMeshPro>();, but it doesn't work. TextMeshPro isn't listed or recognised. How would you attach a TextMesh Pro text to an object's script in the Inspector? Also, how would one enable/disable a TextMeshPro UI object via script?

    Thanks
     
    Last edited: Jan 8, 2019
    Fewpwew130 likes this.
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    You can add this the TMpro namespace to your file:
    Code (CSharp):
    1. using TMPro;
    or you can type
    Code (CSharp):
    1. GetComponent<TMPro.TextMeshPro>();
    Enabling and disabling works for the TMPro Text like every other component,
    you can set enabled to true or false.

    PS: Do you use TextMeshProUGUI (for ui) or TextMeshPro for other usecases?
     
  3. Deleted User

    Deleted User

    Guest

    hi,

    In order to get the text component of TextMeshProUGUI component, I am using GetComponent<TextMeshProUGUI>().text.

    But this is not getting the text.

    How do I get the text?

    Thanks!
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    To get a reference to the <TextMeshProUGUI> component, you would preferably in Awake() or Start() use

    Code (csharp):
    1. GetComponent<TextMeshProUGUI>();
    2.  
    3. // or
    4.  
    5. GetComponent<TMP_Text>();
    which is the base class for both components.

    To get the content of the text, you would use

    Code (csharp):
    1. string text = m_TextComponent.text;
     
    masoudarvishian likes this.
  5. Deleted User

    Deleted User

    Guest