Reputation: 119
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
Reputation: 15941
Canvas
is the parent of the text objectYou need to either use transform.GetChild()
, transform.Find()
or GetComponentInChildren<>()
Upvotes: 3