How can I convert the TextMeshPro text to an int?

I’m having trouble trying to convert from string to int in unity. It keeps giving me FormatExceptions.

In my editor i have dragged a reference to the text object in my scene to the script itself. Its type is TMP_Text. Lets call textObject. When calling the ParseString Method i pass in the textObject.text property since its the string i’m looking for. I have confirmed this with a Debug.Log (which works fine btw)

What i’ve tried so far:

private static int ParseString(string dayText)
    {
        int num = 0;
        num = int.Parse(dayText);
        return num;
    }

This throws a FormatException

private static int ParseString(string dayText)
    {    
        int num = 0;
        if int.TryParse(dayText, out num);
            return num;

        throw new System.ArgumentException("Argument must be a number");
    }

Here the TryParse returns false and the ArgumentException is thrown.

private static int ParseString(string dayText)
    {
        dayText = string.Format("{0}", dayText);

        int num = 0;
        num = System.Convert.ToInt32(dayText);
        return num;

        throw new System.ArgumentException("Argument must be a number!");
    }

Here I get another FormatException.

private static int ParseString(string dayText)
{
    dayText = string.Format("{0}", dayText);

    int num = 0;
    if (int.TryParse(dayText, out num);
        return num;

    throw new System.ArgumentException("Argument must be a number!");
}

I tried using the formatting method which didn’t work. Still throws the ArgumentException.

private static int ParseString(string text)
    {
        string temp = "1";
        temp = temp + text;
        int num = 0;
        if (int.TryParse(dayText, out num);
            return num;

        throw new System.ArgumentException("Argument must be a number!");
    }

I tried some concatination. Still returns false and throws the ArgumentException.

And for the record i tried all the above methods with each of the converting Methods. the other two throws a FormatException instead of the ArgumenteException i wrote.

Additionally i made a test script and tried some of the same conversion using that TMP_Text reference again, but a completely new one, seperate from anything that was already in the scene. Still no conversion on the object, but works fine when i define my own strings somewhere in the class or even change them at runtime with a [SerializedField] private string text;

I tried passing in my own string defined in the method where i use ParseString() and it works as expected. I did the same, defining a field and a property in the class.

I’m starting to believe there is no way around this and i have to find an alternative.
It might just be a problem

3 Answers

3

This seems to be a very annoying issue. The solution to my problem ended up being to use the TMP_InputField directly rather than TMP_Text. For some reason the conversions work then. Its strange because all that does is get get the text stored in its child TMP_Text object… Regardless it did the job for me, maybe it will for you.

I tried this... it didn't work. This is so stupid. I can print out the contents of the .text and it prints "100", so why in the heck can't it be parsed??

–

I tried this and it worked, thanks

–

I got the same issue as well and what did it for me is getting rid of the last character in the string. I think it might be one of the unprintable characters, because when I print out text.text it gives me “123” but when I print out the length it outputs 4.

Hi, instead of dayText use dayText.GetParsedText()

l mean:

int.TryParse(dayText.GetParsedText(), out num)

I've tried this in 2021.3.1f1 and int.Parse functions were still not able to figure out TMP text. Here is my code: public void OnChangeWidth(TMP_Text text) { int value = 0; Debug.Log(text.GetParsedText()); if (text.GetParsedText().Equals("1")) Debug.Log("Equals 1!"); if(int.TryParse(text.GetParsedText(), out value)) { Debug.Log("Parsed value: " + value); } } And here is the output I get: [196240-output.png|196240]

–

I tried this too.... it fails. The text inside is "100" and I can print it out, but TryParse() always returns false. I tried with and without GetParsedText(). I tried with TextMeshProUGUI.text, TMP_Text.text, and TMP_InputField.text ... fail fail fail. Why???

–