Accessing the text and color of a textmeshpro object

Hello everyone,

I am currently trying to change the text and the color of a texmeshpro object.
But for some reason I cannot acces it because some referencing errors occur.
The hierarchy of my GameObject is as followed:
-(emptyGameObject)CubenText
----Canvas
-------Text (TMP)
----Cube

I was able to acces the Cube with the following code:

Cube = CubenText.transform.GetChild(1).gameObject;

and can now change color, size, etc just as I wanted.

But for some reason all my tries to acces the Text (TMP) do not work.
I tried it with

        Tmp = CubenText.transform.GetChild(0).GetChild(0).gameObject;
        Tmp.GetComponent<TextMeshPro>().text = "-4";

but I get the error: Object reference not set to the instance of an object…

Then I thought that I first have to select the Canvas child and then the textmeshpro object with

textmeshPro = CubenText.transform.GetChild(0).gameObject.GetComponentInChildren<TextMeshPro>();

But nothing works.
It my be because of the hierarchy, but I defenitely need both the Cube gameObject and the TextMeshPro object stored together in one emptygameObject.

Does somebody know how to get the Text (TMP) object so I can change the text and color?

Solved!!!

Tmp = CubenText.transform.GetChild(0).GetChild(0).gameObject;
        Tmp.GetComponent<TextMeshProUGUI>().text = "+4";
        Tmp.GetComponent<TextMeshProUGUI>().color = Yellow;

Selecting the Text (TMP) object with the getchildgetchild part.
Accessing the text and color with “TextMeshProUGUI!!!”, because it apparently uses the canvas renderer and i always used the normal “TextMeshPro…”

2 Likes