changing TextMeshPro Text (from UI) via script

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();

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();

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!

9 Likes

The reason you are getting the error is simply because you added a component to the GameObject but you are trying to get a reference to a component.

There are two TMP components who both derive from TMP_Text.

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

The 2nd of type 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.

58 Likes

Ah, thanks, looked over the FAQ earlier, but missed that one.

3 Likes

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?

3 Likes

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#.

13 Likes

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

71 Likes

There are two TextMesh Pro components… the normal component is of type and works with the MeshRenderer and a replacement for the old TextMesh. Then the UGUI component is of type and works with the CanvasRenderer.

Both of these inherit from <TMP_Text>

7 Likes

You can create TextMeshProUGUI object

using UnityEngine;
using TMPro;

public class NumberWizard : MonoBehaviour
{
    [SerializeField] TextMeshProUGUI m_Object;
    
    void Start()
    {
          m_Object..text = "Enter Your Text Here";
    }
}

Now from Unity you have to bind your TextMeshPro object with m_Object.

31 Likes

THANKS!

1 Like

You can watch this video starting at 4:40

3 Likes

I hate video links… but this is awesome. Prefect. Beautiful. Thank you.

I Know I am Too Late For Your Answer But At The First, You Need Add-On The Top (Using TMPro)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class unknown: MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
      
    }

    // Update is called once per frame
    void Update()
    {
      
    }
}

jl;

10 Likes
Using TMPro;
 
public class TextMeshProDemo : MonoBehaviour {
     [SerializeField]
     private TextMeshPro textMeshPro;
   
     void Start() {
          textMeshPro.text = "Some text";
     }
}
9 Likes

6777572--783977--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)

19 Likes

I had multiple TextMeshProUGUI objects, and jhonataneng23’s suggestion is what finally worked for me so I could update both properly.

I don’t know if this is what you needed but I think I have a simple answer.

public TmPro.TextMeshProUGUI text;
text.text = "text"
7 Likes

Both of these examples worked perfectly for me. I don’t know what the difference is.

public TMPro.TextMeshProUGUI myText;

// OR

public TMPro.TMP_Text myText;
2 Likes

what should I be putting in the “using” at the start of my script?

// Both of these are not working;

using TMPro;
using TextMeshPro;

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.

1 Like

You got meeee !!! Thank u sir !!