Whenever I use System.Convert it gives me this error:
"
FormatException: Input string was not in a correct format.
System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Int32.Parse (System.String s, System.IFormatProvider provider) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Convert.ToInt32 (System.String value) (at <695d1cc93cca45069c528c15c9fdd749>:0)
GameLobbyUIBehavior.Update () (at Assets/Scripts/GameLobbyUIBehavior.cs:81)
."
I’ve tried everything I can to fix the errors. I’ve even tried to do it like others, but it still doesn’t work. The ones that worked didn’t assign the value to the int or double at all. I’ve hit a huge road block.
Currently: It seems to be a problem with using TextMeshPro instead of unity’s ui. Whenever you try to use a TextMeshPro input field instead of a unity inputfield it will not be able to convert it to any other value. There’s a way around it by creating both a unity and tmpro input field. Then, create a script that allows you to set the unity input field text to the textmeshpro input field everytime it’s changed and when it starts. Also, you set the alpha value to 0 since you just want textmeshpro. All of this just so you can use TextMeshPro. I don’t know if this is a bug or not.
What was the string you tried to convert and what is the default culture of your device?
This is 99% just an issue of trying to use the default culture of your device with a number formatted for a different culture.
See what characters are being returned when you type a known number, such as “123”.
Try something like:
foreach( var c in resultString)
{
Debug.Log( "[" + c + "]");
}
There might be extra noise in there, such as whitespace.
I tried getting the value directly from TMPro, so it’s probably a problem with how they format their string in an input field.
Okay so here’s the code and what not:
When I try to convert the string from the input field of TMPro to an integer, it fails and just doesn’t convert and gives and error. It just be something like this:
public TextMeshProUGUI inputField;
public int finalValue;
public int returnValue;
if (int.TryParse(inputField.text, out returnValue)) {
finalValue = returnValue;
} else {
Debug.LogError("Conversion failed");
}
The thing that’s wrong is that TMPro doesn’t work and you’d have to change it to:
public InputField inputField;
public int finalValue;
public int returnValue;
if (int.TryParse(inputField.text, out returnValue)) {
finalValue = returnValue;
} else {
Debug.LogError("Conversion failed");
}
The same applies to “Int.Convert”. Everytime, it would apply to else. The value just doesn’t get read.
This is your problem.
You need to be accessing the .text
field of the TMP_InputField
.
You would have noticed this immediately if you dumped the characters as I noted above in response #4, because that would instantly reveal “Hey, there is a unicode 8203 in my text, why?” and google would have answered your question.
https://discussions.unity.com/t/683388
Go fix your reference, use the correct object to get your data.
You’re welcome.
I completely understand what you are saying. Thanks so much! In the future, this will be of great help if I can remember.
string quantityStringFix = _quantityTextInput.text;
quantityStringFix = quantityStringFix.Replace((char) 8203, ' ');
int result = 0;
int.TryParse(quantityStringFix, out result);
_quantity = result;
Where 8203 is unicode symbol of 0 width whitespace