Jim Mede
Jim Mede

Reputation: 53

Unity C# Textmesh pro is not updating when changing text programatically

I have a TextMeshPro object for output and i want to change its text. What i found is that the text is changing but not updating. The weird thing is that i can change it and it updates when it is on start but later on when i try to change it , it does change but do not update. The TextMeshPro object is inside a canvas so i am using an TextMeshProUGUI. TextMeshPro cannot be used cause it does not allow me to drag and drop the object. Tried also delcaring it with get component at start but it gets worse and text dont even change therefore it dont update.

the only way i found is that when changing the width of the rect transform manually then it updates..

found similar discussions but no solution.

https://forum.unity.com/threads/textmesh-pro-is-not-updating-when-changing-text-programatically.490125/

[SerializeField] private TextMeshProUGUI myOutput;


myOutput.text = "msg";    

To recreate the issue. Create a canvas and a TMP inside. Create a script with the code

 [SerializeField] private TextMeshProUGUI myOutput; 

then i added a button, when clicked to call a function where i put

myOutput.text = "msg"; 

Create object to add the script. Add the object to the button and select the function created previously. Used a debug , reaches there but do not updates.

Unity version 2022.3.30f1

Upvotes: 1

Views: 587

Answers (1)

Coco
Coco

Reputation: 55

Here's what you should do.

[SerializeField] public Text myOutput

And then from there you can do

myOutput.text = "Hello World"

Although I believe this is the scripting for a legacy text component, there are very few cases where you should be using a TextMeshPro instead of a legacy text component. Try using the raw/legacy text instead of TextMeshPro and let me know if that works

Upvotes: 0

Related Questions