How does SetText work ?

Hello,

Like some people here my SetText on TextMeshPro UI works fine in the editor but then I build in standalone and update it to Steam and it doesn’t work. The text seems to be only set the first time.

Also about general behaviour : I noticed that calling SetText(“”) to clear the text makes the componen “bug” (maybe it’s expected) and refreshes all the time (with a content fitter preferred size for both) and then calling a SetText with my text is not updating anymore. I might do something wrong. I’m just trying to have a pooling system and empty my texts before putting them in the pool but then when I take them from the pool again and call the SetText it’s never updated. Maybe I do something wrong ? But I couldn’t find much documentation about that (Is it possible to call SetText(“”), should I always activate the gameobject before setting the text ? )

Thanks!

The SetText() function was initially added to allow concatenation of a string with some variable without allocations on the calling side. Eventually, an overload was added to support StringBuilder. So unless you are using the StringBuilder part or need to concatenate some string with some variable, I would suggest using the .text property instead of SetText().

Having said that, SetText() should work so I will need to repro project or some script that I can use to reproduce the behavior reported here.

Hi,

I’ve tried many different ways of doing it but I end up in the same bug:

  • when I set the text to String.Empty it’s not updated. However in the inspector the text property is empty. Just the UI is not refreshed.

.text = String.Empty and SetText(String.Empty) have the same result.

It’s hard to send my whole project. I also call “.ForceMeshUpdate();” just after but that doesn’t help.
Same with ClearMesh();

The only trick i’ve found so far was to set it to " " (one space).

I’m using the example (TMP_TextSelector_B.cs) provided to work with links which uses those 2 functions:
void RestoreCachedVertexAttributes(int index) and changes the color of the letters in ON_TEXT_CHANGED/Late Update.

Maybe there is a bug in the example ?

Can you provide me with a simple script that I can use that will allow me to reproduce the behavior you have on your end?

I have tested this several times on my end and it works as expected. So there is something that I am likely overlooking that is causing the behavior you are seeing on your end.