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. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

TextMesh Pro changing TextMeshPro Text (from UI) via script

Discussion in 'UGUI & TextMesh Pro' started by blubwage, Mar 21, 2017.

  1. blubwage

    blubwage

    Joined:
    Mar 6, 2017
    Posts:
    5
    Hi, might be a stupid question, but I haven't been able to figure out how to change the text for the TextMeshPro Text in my UI via script. I tried:

    TextMeshPro mText = gameObject.GetComponent<TextMeshPro>();

    mText.text = "test";

    gets me the error: Object Reference not set to an instance of an object.

    Same with mText.SetText("test") and

    TextMesh mText = GameObject.GetComponent<TextMesh>();

    mText.text = "agdsf";

    just says, there's no TextMesh attached to the GameObject.

    Script attached to the GameObject and using TMpro. It works if i create it as a 3d object, but not as part of the UI. Not quite sure what I'm missing, looked over the script tutorial for the plugin and there it's just written as stated above. Thanks!
     
    ben4d85, DaniRSH, ishiahirake and 4 others like this.
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,572
    The reason you are getting the error is simply because you added a <TextMeshProUGUI> component to the GameObject but you are trying to get a reference to a <TextMeshPro> component.

    There are two TMP components who both derive from TMP_Text.

    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.
     
  3. blubwage

    blubwage

    Joined:
    Mar 6, 2017
    Posts:
    5
    Ah, thanks, looked over the FAQ earlier, but missed that one.
     
  4. ZoidbergForPresident

    ZoidbergForPresident

    Joined:
    Dec 15, 2015
    Posts:
    157
    So... how do actually edit the text of a textmeshpro control?

    Here's my issue.

    I have a canvas/screen where I edit the data of a member of a list, when I load the screen I fill up the fields with existing data, using a method from my UIManager:
    public void SetUIData(ActionCardItem actionData)
    {
    ActionWhatField.text = actionData.What;
    ActionWhoField.text = actionData.Who;
    ActionWhenField.text = actionData.When;
    ActionRewardField.text = actionData.Reward;
    }
    Action***Field are of the type TextMeshProUGUI.

    The first time it works but when I add a new item in my list and load that one, I get the data from the previous one.

    Is there some special treatement when you try to put an empty screen in the text field of a texxtmeshpro?
     
    Medieval_Maniac and dan_ginovker like this.
  5. Lovecraft-Petrichor

    Lovecraft-Petrichor

    Joined:
    Sep 21, 2014
    Posts:
    2
    To get the component type for something like this, just look at the text label of the component to the left of (Script) and remove the spaces. "Text Mesh Pro UGUI (Script)" as the label of the component reveals that it is a TextMeshProUGUI type. For components that aren't labeled as (Script), just remove the spaces -- "Rect Transform" component is a RectTransform type. For your own scripts -- just make sure you use Pascal case (ex. "PascalCase") for their class names in C#.
     
  6. jasonrbrock

    jasonrbrock

    Joined:
    May 22, 2019
    Posts:
    10
    You Sir are a legend.
    Searched high, low, sideways, diagonal for this solution.

    Was doing my head in....
    transform.GetComponent<TextMesh>().text


    You got me back on-track....
    GetComponent<TMPro.TextMeshProUGUI>().text
     
  7. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,572
    There are two TextMesh Pro components... the normal component is of type <TextMeshPro> and works with the MeshRenderer and a replacement for the old TextMesh. Then the UGUI component is of type <TextMeshProUGUI> and works with the CanvasRenderer.

    Both of these inherit from <TMP_Text>
     
  8. pratikbvmet

    pratikbvmet

    Joined:
    Oct 18, 2019
    Posts:
    3
    You can create TextMeshProUGUI object


    Code (CSharp):
    1. using UnityEngine;
    2. using TMPro;
    3.  
    4. public class NumberWizard : MonoBehaviour
    5. {
    6.     [SerializeField] TextMeshProUGUI m_Object;
    7.    
    8.     void Start()
    9.     {
    10.           m_Object..text = "Enter Your Text Here";
    11.     }
    12. }
    Now from Unity you have to bind your TextMeshPro object with m_Object.
     
  9. rubenbartelet

    rubenbartelet

    Joined:
    Apr 7, 2020
    Posts:
    1
    THANKS!
     
    shawarmaje likes this.
  10. coolamigo

    coolamigo

    Joined:
    Mar 28, 2020
    Posts:
    25
    You can watch this video starting at 4:40
     
    evettsam, Vaupell and Dustin_00 like this.
  11. Dustin_00

    Dustin_00

    Joined:
    Feb 26, 2013
    Posts:
    13
    I hate video links... but this is awesome. Prefect. Beautiful. Thank you.
     
  12. Tamim12321

    Tamim12321

    Joined:
    Jul 30, 2020
    Posts:
    2
    I Know I am Too Late For Your Answer But At The First, You Need Add-On The Top (Using TMPro)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class unknown: MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.      
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.      
    18.     }
    19. }
    jl;
     
    Jofroop, ben4d85, grise_gris and 4 others like this.
  13. codemaker2015

    codemaker2015

    Joined:
    Aug 19, 2018
    Posts:
    26
    Code (CSharp):
    1.    
    2. Using TMPro;
    3.  
    4. public class TextMeshProDemo : MonoBehaviour {
    5.      [SerializeField]
    6.      private TextMeshPro textMeshPro;
    7.    
    8.      void Start() {
    9.           textMeshPro.text = "Some text";
    10.      }
    11. }
    12.  
    13.  
    14.  
     
  14. jhonataneng2332

    jhonataneng2332

    Joined:
    Jan 8, 2021
    Posts:
    4
    upload_2021-1-28_21-6-22.png
    100% tested
    You just need to use it this way and everythingworksfine

    Example:
    missionTitleText = (Same string type variable, or just a string)
     
  15. jan262

    jan262

    Joined:
    Jan 16, 2021
    Posts:
    9
    I had multiple TextMeshProUGUI objects, and jhonataneng23's suggestion is what finally worked for me so I could update both properly.
     
  16. AurenJuice

    AurenJuice

    Joined:
    Apr 19, 2021
    Posts:
    1
    I don't know if this is what you needed but I think I have a simple answer.

    Code (CSharp):
    1. public TmPro.TextMeshProUGUI text;
    2. text.text = "text"
     
  17. Greenwater79

    Greenwater79

    Joined:
    Nov 20, 2021
    Posts:
    8
    Both of these examples worked perfectly for me. I don't know what the difference is.

    Code (CSharp):
    1.  
    2. public TMPro.TextMeshProUGUI myText;
    3.  
    4. // OR
    5.  
    6. public TMPro.TMP_Text myText;
    7.  
    8.  
     
    samana1407 likes this.
  18. BlackRece

    BlackRece

    Joined:
    Aug 26, 2017
    Posts:
    1
    what should I be putting in the "using" at the start of my script?

    Code (CSharp):
    1. // Both of these are not working;
    2.  
    3. using TMPro;
    4. using TextMeshPro;
     
  19. Code_Crafter

    Code_Crafter

    Joined:
    Mar 13, 2022
    Posts:
    1
    Declaring your variable to be a type of
    TextMeshProUGUI
    will work, but only if you have included
    using TMPro;
    at the beginning of your script.
     
    HtPsI likes this.
  20. gmerkhaled

    gmerkhaled

    Joined:
    Sep 30, 2022
    Posts:
    1
    You got meeee !!! Thank u sir !!
     
  21. triangle4studios

    triangle4studios

    Joined:
    Jun 28, 2020
    Posts:
    25
    If you are like me and find the name "TextMeshProUGUI" syntactically ugly, I created a quick workaround that turns it into just plain "Text":


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace TMPro
    4. {
    5.     [DisallowMultipleComponent]
    6.     [RequireComponent(typeof(RectTransform))]
    7.     [RequireComponent(typeof(CanvasRenderer))]
    8.     [AddComponentMenu("UI/Text Mesh Pro/Text", 0)]
    9.     [ExecuteAlways]
    10.     public class Text : TextMeshProUGUI
    11.     {
    12.  
    13.     }
    14. }
    Throw it on a script name Text.cs and put it somewhere in your assets folder.

    You can access it like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using TMPro;
    3.  
    4. public class Test : MonoBehaviour
    5. {
    6.     public Text t;
    7.     void Start()
    8.     {
    9.         t.text = "THIS IS A TEST";
    10.         t.color = Color.red;
    11.     }
    12. }
    And honestly Unity, "TextMeshProUGUI" is neither memorable nor easy on the eyes. Perhaps this workaround should be polished, bug tested and made official.

    Cheers.
     
    Last edited: Nov 9, 2022
  22. PurTom

    PurTom

    Joined:
    Nov 5, 2022
    Posts:
    1
    Can you explaing for me in a bit more details how can i select diffrent textmeshpro texts?
     
unityunity