Search Unity

Bug TMP TextInfo not updating instantly after changing TMP Text

Discussion in 'UGUI & TextMesh Pro' started by WolfVD, Aug 26, 2020.

  1. WolfVD

    WolfVD

    Joined:
    Jul 11, 2020
    Posts:
    1
    The title.

    I came across this bug with a code segment similar to the following where inputField is of type TMP_Text and otherString is a regular string (I didn't test out this particular piece of code, just used for demonstration purposes):
    Code (CSharp):
    1. inputField.text = inputField.text + otherString;
    2.  
    3. Debug.Log(inputField.textInfo.characterInfo[inputField.text.Length-1].character);
    This piece of code will print out a blank line. To get the Debug.Log to work correctly, it must be in Update and will usually register the change to TMP_Text after one or two Update calls.

    An easy issue to work around, but annoying nonetheless.
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Whenever properties of the text object change, the text object will be processed / regenerated which happens late in the update cycle and just before the camera is rendered. When the text object is updated, the content of the textInfo will be updated to reflect these changes.

    For most use case, this is fine but in some cases where you need access to the text info right away, you can use ForceMeshUpdate() which has several overloads to force an update of the text object which will result in the textInfo being updated.
     
    browne11 likes this.