how to change text mesh pro text unity code example

Example 1: how to edit text mesh pro text

using UnityEngine;
using System.Collections;
using TMPro;

public class ExampleClass : MonoBehaviour
{
	public TextMeshProUGUI textDisplay;
    
    void Example()
    {
        textDisplay.text = "Example Text"      
    }
}

Example 2: change textmesh pro text

using UnityEngine;
using System.Collections;
using TMPro;

public class ExampleClass : MonoBehaviour
{
    void Example()
    {
        TextMeshPro textmeshPro = GetComponent<TextMeshPro>();
        textmeshPro.SetText("The first number is {0} and the 2nd is {1:2} and the 3rd is {3:0}.", 4, 6.345f, 3.5f);
        // The text displayed will be:
        // The first number is 4 and the 2nd is 6.35 and the 3rd is 4.
    }
}

Example 3: unity change tmp text from script

using UnityEngine.UI;
using TMPro;

void Update()
{
    TextMeshPro lemonsss = GetComponent<TextMeshPro>();
}

Example 4: unity get textmesh pro component

GetComponent<TMPro.TextMeshProUGUI>().text

Example 5: how to change textMesh Pro unity

// If you are trying to chnage the text on a TextMeshPro:
  
public TextMeshPro TextMeshProObject;
//you shouldnt need to get component the editor should take care of this for you when 
//you drop it since you have the object set to TextMeshPro and not just GameObject
TextMeshProObject = GetComponent<TextMeshPro>();
TextMeshProObject.text = "Hello";

// If you are trying to change the text from a gameobject:
TextMeshProUGUI TextMeshProLable = YourGameObject.GetComponent<TextMeshProUGUI>();
TextMeshProLable.text = "Your Text"

Tags:

Misc Example