Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved I can't use Parse, TryParse, or Convert to change a string to an int and double.

Discussion in 'Scripting' started by MrAquaDude, Sep 17, 2021.

  1. MrAquaDude

    MrAquaDude

    Joined:
    Feb 11, 2021
    Posts:
    13
    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.
     
  2. MrAquaDude

    MrAquaDude

    Joined:
    Feb 11, 2021
    Posts:
    13
    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.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,932
    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.
     
    Bunny83 likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,065
    See what characters are being returned when you type a known number, such as "123".

    Try something like:

    Code (csharp):
    1. foreach( var c in resultString)
    2. {
    3.   Debug.Log( "[" + c + "]");
    4. }
    There might be extra noise in there, such as whitespace.
     
  5. MrAquaDude

    MrAquaDude

    Joined:
    Feb 11, 2021
    Posts:
    13
    I tried getting the value directly from TMPro, so it's probably a problem with how they format their string in an input field.
     
  6. MrAquaDude

    MrAquaDude

    Joined:
    Feb 11, 2021
    Posts:
    13
    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:
    Code (CSharp):
    1. public TextMeshProUGUI inputField;
    2.     public int finalValue;
    3.     public int returnValue;
    4.     if (int.TryParse(inputField.text, out returnValue)) {
    5.         finalValue = returnValue;
    6.     } else {
    7.         Debug.LogError("Conversion failed");
    8.     }
    The thing that's wrong is that TMPro doesn't work and you'd have to change it to:
    Code (CSharp):
    1. public InputField inputField;
    2.     public int finalValue;
    3.     public int returnValue;
    4.     if (int.TryParse(inputField.text, out returnValue)) {
    5.         finalValue = returnValue;
    6.     } else {
    7.         Debug.LogError("Conversion failed");
    8.     }
    The same applies to "Int.Convert". Everytime, it would apply to else. The value just doesn't get read.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,065
    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://forum.unity.com/threads/textmesh-pro-ugui-hidden-characters.505493/

    Go fix your reference, use the correct object to get your data.

    You're welcome. :)
     
    JadsonAlmeida likes this.
  8. MrAquaDude

    MrAquaDude

    Joined:
    Feb 11, 2021
    Posts:
    13
    I completely understand what you are saying. Thanks so much! In the future, this will be of great help if I can remember.
     
    Kurt-Dekker likes this.
  9. ilya_yurin407

    ilya_yurin407

    Joined:
    Apr 29, 2021
    Posts:
    1
    Code (CSharp):
    1.             string quantityStringFix = _quantityTextInput.text;
    2.             quantityStringFix = quantityStringFix.Replace((char) 8203, ' ');
    3.             int result = 0;
    4.             int.TryParse(quantityStringFix, out result);
    5.             _quantity = result;
    Where 8203 is unicode symbol of 0 width whitespace
     
    moshecristel likes this.