Search Unity

ToString() issue with TMPro conversion

Discussion in 'UGUI & TextMesh Pro' started by tomraegan, Jun 25, 2022.

  1. tomraegan

    tomraegan

    Joined:
    Mar 28, 2016
    Posts:
    137
    Hi!
    This system uses the old Unity text, which I've been updating to TMPro. It runs perfectly with the old Unity Text in place. No errors and correct functionality. But since converting to TMPro (with no other changes) I'm getting a runtime error: NullReferenceException: Object reference not set to an instance of an object

    The relevant TMPro object is placed inside the public variable stackText.

    Here are the relevant pieces of code:
    Code (CSharp):
    1.  
    2. using TMPro;
    3.  
    4. public TextMeshProUGUI stackText;
    5.  
    6.     public void SetStack(int stack)
    7.    {
    8.        this.stack = stack;
    9.        if (stack == int.MaxValue)
    10.        {
    11.            stackText.text = "∞";
    12.        }
    13.        else
    14.        {
    15.            stackText.text = this.stack.ToString(); // this is the line throwing the error
    16.         }
    17.        UpdateCondition();
    18.    }
    I'm struggling to convert the int to a string. I've done plenty of research over the last few hours without success. Would love someone to hint at why it's throwing the error.

    Thank you! (This looks like a scripting problem, but the code is correct (I think)...my guess is it's a TMP specific quirk...hence this forum.)
     
    Last edited: Jun 26, 2022
  2. tomraegan

    tomraegan

    Joined:
    Mar 28, 2016
    Posts:
    137
    I've got it sorted...I used the Text to TMPro converter on it in another module and it did a better job than my manual attempt. Copied it over and all is well.