Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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!
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    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?
     
  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:
    15
    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

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    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:
    14
    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;
     
    Leo2236, dfm31, andriuspp and 7 others like this.
  13. codemaker2015

    codemaker2015

    Joined:
    Aug 19, 2018
    Posts:
    27
    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.  
     
    PhiDeltaZan and samana1407 like this.
  18. BlackRece

    BlackRece

    Joined:
    Aug 26, 2017
    Posts:
    4
    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:
    33
    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
    LibertyNoob likes this.
  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?
     
    hemanth17 likes this.
  23. jaithehuman

    jaithehuman

    Joined:
    Dec 21, 2022
    Posts:
    1
    Omg thank you so much, i probably would have wondered forever why my game wasnt working if you werent here, thank you so much
     
  24. unity_C499BA9FBC6D0148609C

    unity_C499BA9FBC6D0148609C

    Joined:
    Jan 31, 2023
    Posts:
    1
    Code (CSharp):
    1.  
    2. using TMPro;
    3. private TMPro.TextMeshProUGUI text;
     
    snr10 likes this.