Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    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

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    4,949
    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.
     
    indiewan and algorithm-planet like this.
  4. ZoidbergForPresident

    ZoidbergForPresident

    Joined:
    Dec 15, 2015
    Posts:
    136
    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:
    1
    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#.
     
    ilolaaiden, Arrawn, Arfiikins and 2 others like this.
  6. jasonrbrock

    jasonrbrock

    Joined:
    May 22, 2019
    Posts:
    8
    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:
    4,949
    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
  10. coolamigo

    coolamigo

    Joined:
    Mar 28, 2020
    Posts:
    24
    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:
    12
    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;
     
    Gillysuit902 likes this.
unityunity