user1639348
user1639348

Reputation: 119

How to access TextMeshProUGUI in Unity

I have a Canvas with a button and a "TextMeshPro - Text" component. When I click the button I want to update the text of the textMeshPro component. This is the code I'm using:

var canvas = GameObject.Find("Canvas");
var tmp = canvas.GetComponent<TextMeshProUGUI>();
Debug.Log(tmp);

tmp is null but why? In the inspector I see it's an "RectTransform" with "Text Mesh Pro UGUI (Script)". I can access the RectTransform in the script but not the TextMeshProUGUI component

Upvotes: 2

Views: 4835

Answers (1)

Canvas is the parent of the text object

You need to either use transform.GetChild(), transform.Find() or GetComponentInChildren<>()

Upvotes: 3

Related Questions