unity change text mesh pro text in script 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: what to include if using text mesh pro

using UnityEngine
// Include the name space for TextMesh Pro
using TMPro;
public class MyClass : MonoBehaviour
{
  private TMP_Text m_TextComponent;

  private void Awake()
      {
          // Get a reference to the text component.
          // Since we are using the base class type <TMP_Text> this component could be either a <TextMeshPro> or <TextMeshProUGUI> component.
          m_TextComponent = GetComponent<TMP_Text>();

          // Change the text on the text component.
          m_TextComponent.text = "Some new line of text.";
      }
}

Example 3: unity get textmesh pro component

GetComponent<TMPro.TextMeshProUGUI>().text

Example 4: 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