Search Unity

TextMesh Pro Textmesh pro is not updating when changing text programatically

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by Ledrec, Aug 24, 2017.

  1. Ledrec

    Ledrec

    Joined:
    Sep 10, 2014
    Posts:
    8
    There are several things that happens with this error:

    1.- Text Mesh Pro is not updating text even though i use forceMeshUpdate to do it.
    • I make a change on the text with my code like this:
    balloons.GetChild(0).GetChild(0).GetComponent<TextMeshProUGUI>().SetText(currentConversation.PartnerNodeText();

    In the next image you can see that the component text value is changing, and that in the scene said value is not being shown

    Screen Shot 2017-08-24 at 10.02.35 AM.png
    • the component textmeshpro UGUI is showing the correct text, but i cant see it in the game.
    2.- im using is TextTruncated to activate other objects in scene and change the margin and local position of my textmeshpro object and using ForceMeshUpdate, to see the changes, which are not happening, so textTrucated stays in false.
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,192
    There could be any number of things affecting this including the use of isTextTruncated which is readonly where it should not be changed. This is used by TMP when parsing the text to deal with things like ellipsis, truncation and other things and would affect updates of the text object. You should be able to do all the changes you describe with regards to margin position and all without touching isTextTruncated.

    Second, try using the plain .text property instead of SetText to see if you get the same behavior?
     
  3. Ledrec

    Ledrec

    Joined:
    Sep 10, 2014
    Posts:
    8
    Hi thanks for your reply!!
    I've tried the .text property and SetText(); none of them seems to change my text as i wanted.
    were not changing isTextTruncated, we're just checking if it gets truncated for something to happen...

    i just think its not updating the text since it gets shown in the inspector, what do you think?
     
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,192
    I would have to get a repro of this to take a closer look. If you can provide me with a repro, make sure that you do so privately via some link provided to me in a Private Message (PM).
     
  5. Ledrec

    Ledrec

    Joined:
    Sep 10, 2014
    Posts:
    8
    OK ill do it, let me prepare the scene.
     
  6. Ledrec

    Ledrec

    Joined:
    Sep 10, 2014
    Posts:
    8
    I already fixed it, by moving the gameobject to another position :/ dont know why it works but it does.
     
  7. LesBloom

    LesBloom

    Joined:
    Feb 2, 2017
    Posts:
    72
    I just encountered this same issue. I am only using the .text value to update the string. I have "Wrapping & Overflow" disabled. Disabling "Auto Size" for Font Size was the only way I could get the display to update.

    Thanks
     
  8. Xander_

    Xander_

    Joined:
    Jul 4, 2013
    Posts:
    2
    Hi,
    Any updates about this? I use TMP and get this same issue \n wont work until you manually click something on script.
     
  9. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,192
    This is likely something else so I will need more details.

    Unity in any public fields or in the Inspector escapes control characters like "\n" which results in these being converted into a string literal or a "\" + "n" instead of the linefeed which is char (10).

    I suspect the issue you are having is potentially related to this but like I said I will need more. Do you have some example script that reproduces the behavior or can you provide a Repro project?
     
  10. Xander_

    Xander_

    Joined:
    Jul 4, 2013
    Posts:
    2
    I can deliver some if you want to fix this.
    For now i replace \n with <br> and works.
     
    Bart_B likes this.
  11. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,192
    I got your PM and will take a look at your repro but like I said this is likely the result of your /n being escaped.
     
  12. WorldEater

    WorldEater

    Joined:
    Oct 23, 2014
    Posts:
    2
    I also encountered this issue, after som testing it seems to have something to do with an animator animating the alpha of the text. I was however not able to replicate the issue on another object. I finally rolled back to the latest commit and have not encountered it since.

    When testing I also noticed that when making changes while paused and then unpausing the changes were applied as expected but not in realtime. Very strange issue.
     
  13. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,192
    Just let me know if you run into that behavior again. Most of the time reproducing issues is the hardest part.
     
  14. Ledrec

    Ledrec

    Joined:
    Sep 10, 2014
    Posts:
    8
    Hi Stephan_B it happened again, i even used force text update and its no working...
     
  15. MacFBG

    MacFBG

    Joined:
    Jan 23, 2016
    Posts:
    18
    Can confirm I'm getting this issue.

    I've got two types of enemies set up, ones that move and ones that don't. They both share the same Animator Controller, so the animations being called to alter the text is identical. The animation simply moves the alpha up or down depending on if I'm showing or hiding the enemies name.

    On the enemies that move, this works without any issue. On the stationary enemy the text is never visible, despite being in the correct animation and the alpha having been moved up. If I pause the game and alter the text in editor, by for example deleting one letter then the text appears.

    I'm assuming that because one enemy is moving they are always having to redraw the text where as the stationary one must not be noticing a change to the asset. It would be good to get a fix for this.
     
  16. MacFBG

    MacFBG

    Joined:
    Jan 23, 2016
    Posts:
    18
    I found a solution to this issue that works for my particular issue.

    Code (CSharp):
    1. public TexhMeshPro Name;
    2.  
    3. public void SetText(string nameToSet)
    4. {
    5.      Name.text = nameToSet;
    6.      Name.ForceMeshUpdate(true);
    7. }
    Crucially it's the ForceMeshUpdate call that fixes the issue.
     
    elek likes this.
  17. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,192
    Unless a property of the text object changes, moving the object around should not trigger a regeneration. If the scale of the text object changes, this would trigger a regeneration as the SDF scale needs to be updated.

    When a text object is affected by an animation, update of the text object is driven by the OnDidApplyAnimationProperties() callback in the case. For the legacy animation system which doesn't have a callback, you have to set the "isUsingLegacyAnimationComponent" property to true.

    I think the difference in behavior is related to this but I would need some repro scene or project to figure out exactly why.

    ForceMeshUpdate() is forcing the object to be regenerated so if the object wasn't aware that it should be updates because of changes coming from the legacy animation, that would work.
     
  18. pako

    pako

    Joined:
    Nov 21, 2012
    Posts:
    80
    Hi,

    I've run into a strange situation...

    I use 2DToolkit in my project, and I'm in the process of replacing all instances of tk2dTextMesh with TextMesh Pro.

    Things were going relatively smoothly, until I needed to change Wrapping & Overflow in one TMP instance from the default Overflow to Truncate. I found the following behaviour:

    - When I duplicate the GameObject with the TMP component, the clone displays no text in the scene, even though the Text Input Box contains text. Additionally, although the original GameObject displays text properly in the scene, after I unload and reload the scene, it too, does not display in the scene the text in its Text Input Box.
    - Switching back to Overflow restores visibility of the existing text, but if I switch to Truncate again, and type extra characters in the Text Input Box, the extra characters are not displayed in the scene. However, after adding enough (or too many) characters, much beyond what would cause a truncation, all characters disappear from the scene. They reappear after I switch to Overflow.
    - While in Overflow, if too many characters are added, and characters do get overflown to the next line, switching to Truncate does nothing. In fact, switching to any other Wrapping & Overflow setting has not effect, and TMP becomes unresponsive, as far as displaying text properly in the scene is concerned. Setting Overflow again restores functionality.

    I created a new empty project and imported TextMesh Pro and 2DToolkit. I created the same font asset as in my project, replaced the default camera with tk2dCamera, and used these for testing. There was no problem! Wrapping & Overflow functionality was correct with all settings.

    So, it must be something specific to my project, but I'm at a loss of what to do to fix it.

    I use Unity 2017.2.1f1 and TextMesh Pro 1.2.2

    Any ideas?

    EDIT: I created a repro case - Just a GameObject with TMP component and Main Camera in the scene, and only TMP and the custom font asset in the Project Assets. I reproduced all of the above behaviour.
     
    Last edited: Aug 5, 2018
  19. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,192
    Can you provide me with the repro project / case?
     
  20. pako

    pako

    Joined:
    Nov 21, 2012
    Posts:
    80
    Yes, started a conversation and shared download link.
     
  21. brinca

    brinca

    Joined:
    Mar 29, 2015
    Posts:
    13
    I was having a similar problem with a custom editor, where the text would not update immediately if called from a button press on an OnInspectorGUI call, and SetText() or ForceMeshUpdate(true) didn't do anything, but you can trigger a refresh manually simply by setting the game object inactive and then active again:
    Code (CSharp):
    1. label.gameObject.SetActive(false);
    2.  
    3. label.text = "Whatever";
    4.  
    5. label.gameObject.SetActive(true);
     
    pako likes this.
  22. pako

    pako

    Joined:
    Nov 21, 2012
    Posts:
    80
    Thanks @brinca I'll keep it in mind next time I need it. For now, I just use the default Overflow setting. Unfortunately, I still have not received a response from Stephan even though almost 2 months have passed since I uploaded the repro project. So, it's good to have a quick fix at hand. Thanks again!
     
  23. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,192
    Sadly, I forgot or simply missed your PM. My apologies for that.

    Since I do get a significant amount of emails and PMs, please never hesitated to post a reminder or PM me again when I fail to reply within a few days.

    I'll try taking a look at your PM / repro over the weekend (assuming the download link is still valid).
     
    pako likes this.
  24. pako

    pako

    Joined:
    Nov 21, 2012
    Posts:
    80
    OK thanks! I didn't post a reminder because I didn't want to give the impression of nagging.

    Also, I just realized that my post is not really relevant to this particular thread. I should have created a new thread. I don't know how I missed this thread's title... I wish I could correct this even now, but I'm not quite sure what would be the best way to do it. Maybe post my question as a new thread and delete it form here or something?
     
  25. karmington2

    karmington2

    Joined:
    Jul 30, 2018
    Posts:
    1
    I am seeing some resizing problems also ( Unity 2018.2.8f1, in-built TMPro )
     

    Attached Files:

  26. samlabs_lea

    samlabs_lea

    Joined:
    May 1, 2018
    Posts:
    7
    We are experiencing the exact same issue as well (version that is built-in to Unity 2018).

    The suggestion to disable the game object, adjust the text, and then re-enable the game object seems to workaround the issue. But this is a very hacky fix.
     
  27. ZyngaStef

    ZyngaStef

    Joined:
    Oct 9, 2014
    Posts:
    2
    For me the ForceMeshUpdate worked out and the setActive trick did not. On further investigation I notice the animation was setting this property

    Screen Shot 2018-10-04 at 12.26.00 PM.png

    Removing it fixed the issue.
     
  28. snw

    snw

    Joined:
    Mar 13, 2014
    Posts:
    31
    Any update/fix on this?

    The workarounds mentioned above do not work for me.

    My scenario:
    - InputField is a nested child of a parent.
    - Parent gets disabled.
    - I need to update the InputField while parent is disabled.

    Update:
    Nevermind. I was setting the text on the TextMeshProUGUI directly. Setting the text on the InputField instead works as expected.


    Cheers.
     
    Last edited: Oct 16, 2018
    Stephan_B likes this.
  29. GargamelSE

    GargamelSE

    Joined:
    Oct 19, 2018
    Posts:
    3
    I have recently started to make a game, and i cannot for my life change the text in the textmesh pro items on the game, at all.

    im trying to like this:

    Code (CSharp):
    1. TextMeshProUGUI _CrindarsValue = GameObject.Find("CrindarsValue").GetComponent<TextMeshProUGUI>();
    2.  
    3.         _CrindarsValue.enabled = false;
    4.         _CrindarsValue.SetText(GameValues.Crindars.ToString("#.00"));
    5.         //_CrindarsValue.text =GameValues.Crindars.ToString("#.00");
    6.         _CrindarsValue.ForceMeshUpdate(true);
    7.         _CrindarsValue.enabled = true;
    8.  
    But the value on screen still stays 0, both in the editors play function and a fully built and run in the browser (webgl)
     
    Drago28 likes this.
  30. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,192
    Just use the normal .text property to change the text.

    Install the TMP Examples and Extras from the Window - TextMesh Pro menu and take a look at the multiple examples like Simple Script which do change the text.
     
  31. GargamelSE

    GargamelSE

    Joined:
    Oct 19, 2018
    Posts:
    3
    Hi, i have tried that too ofcourse (you can see the commented out .text).
    But the text stays still as 0 (default text from editor)
    Neither in editor play or compiled webgl shows any difference on the text.
    Its just like the text is not redrawn, i make some calc or updates with the updatE() (each frame) function, maybe its why it doesnt work?
     
  32. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,192
    Can you provide a simple script that will help me reproduce this behavior?
     
  33. lovemoebius

    lovemoebius

    Joined:
    Dec 12, 2016
    Posts:
    85
    I don't want to be that guy but I've been struggling with TextMeshPro all day for incredibly simple stuff such as changing height and width or my Text not updating (which is what landed me here) and after googling different issues for about 6 hours there's one thing that is fairly clear, these docs are definitely not enough.

    I'm unsure how official TMP is to Unity at this point. Unity clearly pushes you towards using it and there are people that I think work for the company that made it that are staff here, however the Unity docs really don't have any information about it whatsoever.

    Just going to suggest that some kind of Scripting documentation or even slightly more in depth docs about how to actually use TMP would be an absolute godsend and would cut a lot of time off having to decipher questions asked on the company's forum 4 years ago that might not even work anymore.

    Or, even better, make TMP an official part of Unity and give it its own space in the official Documentation.

    Edit: I found this, but either it doesn't work or is massively updated for example. Dunno.
     
    Last edited: Nov 2, 2018
  34. Entevily

    Entevily

    Joined:
    Jun 16, 2014
    Posts:
    2
    This post seems old, but something similar is happening to me now it would seem.

    Not sure the cause and I've tried multiple things mentioned on this page with no luck.

    My Version of Unity/Text Mesh Pro
    upload_2019-1-21_11-22-32.png

    I have a Button that's registered to an event that calls a method in my script that simply logs the text.text to the console, then checks if "text.text" is empty or not, then sends that text via a chat client, and then clears the text with text.text = "".

    Code (CSharp):
    1.  
    2. public void SendMessage()
    3. {
    4.     Debug.Log(text.text);
    5.     if (!string.IsNullOrEmpty(text.text.Trim()))
    6.     {
    7.         // THIS SHOULDN'T BE CALLED WHEN THE TEXT BOX IS BLANK //
    8.         Client.Instance.Lobby.SendChatMessage(text.text);          
    9.     }
    10.     text.text = "";
    11. }
    12.  
    Strangely, after doing this, the text box in unity still shows the text written previously and was not reset to an empty string "". The text.text ="" seems to be ignored completely. If I hit the button again, it sends that same string. Once I manually change the text box to an empty screen, it seems to be empty via the console.log but somehow the string gets by my check for an empty string, and sends out a blank chat message anyways.

    See below gif. This shows the SendMessage command above being executed over and over again with different scenario's and it just is not acting correctly.

    unity.gif

    After manually deleting the text in the text box and hitting send again it goes through and logs text.text, which is blank. But then my check to see if the string is null or empty is ignored and it sends the blank text. Strangely the blank text, is not blank when it's sent.
     
  35. Entevily

    Entevily

    Joined:
    Jun 16, 2014
    Posts:
    2
    Sorry for posting this, found out my issue was above was simply because I was trying to use the text box itself instead of the Text Mesh Pro Input Field. In essence, I was resetting the value of the text box inside of the Input Field Object, but TMPRO uses the Input Field Object itself to interact with the text properly.

    Simply changed my text variable to be a of type TMP_InputField and assigned it to that Input Field parent instead of the text child I was manipulating previously.
     
    Stephan_B likes this.
  36. Emanx140

    Emanx140

    Joined:
    Jan 13, 2017
    Posts:
    3
    Hi!
    Nothing above didn't help me.

    For me, the issue was that I tried to set text in a separate thread(not Main thread). So I just make a simple bool flag that text needs to be updated. And update it in the Update method.
    Code (CSharp):
    1.  
    2. public void UpdateCurrentUser(User user)
    3. {
    4.      currentUser = user;
    5.      updateText = true;
    6. }
    7. private void Update()
    8. {
    9.     if (updateText)
    10.     {
    11.          text.text = currentUser.stars.ToString();        
    12.          updateText = false;
    13.     }
    14. }
    15.  
    Hope it can help someone.
     
  37. PeterAshford

    PeterAshford

    Joined:
    Mar 4, 2019
    Posts:
    2
    I had the same issue and none of the above solutions worked for me. My clever (lucky) workmate suggested clearing the text field first - just using thing.text ="" and then setting the value I wanted. This worked! Maybe it will for you too