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
Answer by Skyppex_ · May 12 at 07:40 AM
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.
Answer by Nazirzadeh · Sep 15, 2021 at 11:43 PM
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:
Hey i posted an answer that worked for me, check if it work for you as well :) Hopefully it does…