Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved TMP_TextInfo.pageCount returns zero

Discussion in 'UGUI & TextMesh Pro' started by bogheorghiu, Jan 19, 2021.

  1. bogheorghiu

    bogheorghiu

    Joined:
    Oct 14, 2018
    Posts:
    9
    Solved.
    Former tile: TMP_TextInfo.pageCount returns zero when static method is involved

    (former title wrong since this is not what was causing the problem. what follows is the original post, see the reply for the solution)

    Here are the snippets of code that are causing the problem:

    Code (CSharp):
    1. public static void WriteLine(string line)
    2. static string upperBoxString;  
    3. public static TMP_Text upperTMPComponent;
    4. {
    5.         upperBoxString += line + '\n';
    6.         upperTMPComponent.gameObject.GetComponent<RefreshUpperBox>().Refresh(upperBoxString);
    7. }
    Code (CSharp):
    1. public class RefreshUpperBox : MonoBehaviour
    2. {
    3.     TMP_Text tmpComponent;
    4.  
    5.     private void Awake()
    6.     {
    7.         tmpComponent = gameObject.GetComponent<TMP_Text>();
    8.     }
    9.  
    10.     public void Refresh(string lines)
    11.     {
    12.         tmpComponent.text = lines;
    13.  
    14.         int pageCount = tmpComponent.textInfo.pageCount;
    15.         Debug.Log(pageCount);
    16.         Debug.Log(GetComponent<TMP_Text>().textInfo.pageCount);
    17.         Debug.Log(GetComponent<TMP_Text>().text);
    18.     }
    There are more lines of code further down in the Refresh method but it's not helpful to paste them here.
    It all relies on pageCount.

    The first Debug.Log returns 0, and so does the second one (I tried both ways of writing the same thing, just in case). The third Debug.Log returns the actual text, so the reference isn't broken. The inspector window set to debug mode shows pageCount correctly, the problem is only in code.

    This doesn't happen when I call the same Refresh() method on the same class instance from elsewhere:

    Code (CSharp):
    1. private void SendCommandLine(string line)
    2.     {
    3.         NotifyCommandHandlers(line);
    4.  
    5.         playerInputText = "";
    6.  
    7.         RefreshLowerBoxText();
    8.         lowerTMPComponent.ForceMeshUpdate();
    9.         upperTMPComponent.gameObject.GetComponent<RefreshUpperBox>().Refresh(upperBoxString);
    10.         upperTMPComponent.ForceMeshUpdate();
    11.  
    12.         upperTMPComponent.pageToDisplay = upperTMPComponent.textInfo.pageCount;
    13.     }
    Everything was working fine until I moved that bit of code to the static method, for code efficiency. Before that, it was being called in Update() and it worked. So I don't know if the method being static is the problem or not, but it seems so.
     
    Last edited: Jan 22, 2021
  2. bogheorghiu

    bogheorghiu

    Joined:
    Oct 14, 2018
    Posts:
    9
    this wasn't a TMPro issue, nor did it have anything to do with static methods.

    it was simply caused by my incomplete understanding of how TMPro works.
    the idea being that it updates the textInfo property only when the mesh is updated, that is either in Update() or by calling ForceMeshUpdate() on the TMP_Text component.

    this is what had actually changed in my code, the static method part is irrelevant.
     
  3. Ubrano

    Ubrano

    Joined:
    Jul 23, 2017
    Posts:
    16
    It seems reasonable - particularly, if one is feeding text content dynamically - to wait for the content to load and/or become available, thus wait at least until the end of frame before doing further stuff on the back of textInfo.pageCount; otherwise, the count would regularly produce whatever the previous count was or alternatively 1, if there’s no content.