How can you enable/disable TextMeshPro text in a script?

Like the title asks…

Regular text can be enabled using (placetexthere).enabled = true, but how can you do it for TextMesh Pro text?

1 Like

TextMeshPro components come with a MeshRenderer on the same object. You can set “meshRenderer.enabled = true/false”. TextMeshProUGUI components can directly be enabled/disabled using “textMeshProUGUI.enabled = true/false”.

9 Likes

Great. Thank you. :slight_smile:

I still seem to be having trouble with this. I’m following this tutorial, but trying to do it a bit different…

Instead of having the text appear straight away on Start, I want it to trigger after pressing a key. But even using my reference name - textDisplay - enabling by putting textDisplay.enable = true isn’t working. It still won’t enable after pressing the key. :-\

The GameObject, the TextMesh component, AND the MeshRenderer will all need to be enabled.

I don’t have enough info to know what problem you are having now, but my guess is you are turning off the GameObject to hide the text, then you are trying to enable the MeshRenderer to show the text. Whatever you did to hide the text, you will have to do the reverse to show it. Try textDisplay.gameObject.SetActive(true); after your textDisplay.enable = true;

If that doesn’t work, could you post your script?

11 Likes

Perfect. Thank you. Yeah, I was disabling the GameObject itself instead of the Text Mesh Pro UGUI (Script) component. All sorted now. :smile:

FindObjectOfType().enabled = false; can be used inside of functions

You can just make a second variable for it as a GameObject. For Example:

[SerializeField] TextMeshProUGUI speech;
[SerializeField] GameObject speechObject;

speechObject.SetActive(false);

in unity you would simply set both variables equal to the same object in the hiearchy. Alternatively you could set the text to " " (nothing)