Search Unity

Resolved Text error?

Discussion in 'Scripting' started by Nixel2013, Oct 21, 2020.

  1. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    Error:
    System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) (at <fb001e01371b4adca20013e0ac763896>:0)
    System.Int32.Parse (System.String s) (at <fb001e01371b4adca20013e0ac763896>:0)
    ResuelveElProblema.Update () (at Assets/ResuelveElProblema.cs:44)


    Code:
    Code (CSharp):
    1.    
    2.     public TMP_InputField CasillaUno;
    3.     public TMP_InputField CasillaDos;
    4.  
    5.  
    6.     [Space(40)]
    7.  
    8.     public int intCasillaUno;
    9.     public int intCasillaDos;
    10.  
    11.  
    12. void Update()
    13.     {
    14.         intCasillaUno = int.Parse(CasillaUno.text);
    15.         intCasillaDos = int.Parse(CasillaDos.text);
    16.  
    17.     }


    It does not cause any problem in the project, but I would like to know if there is any way that this error does not appear. It's just for sheer aesthetics in the project nomas, thanks.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    You are probably reading blank strings from the InputFields which obviously cannot be parsed to int. Just put something like
    Code (CSharp):
    1. if (CasillaUno.text.Length > 0)
    around the code. Or use int.TryParse instead.
     
    Nixel2013 likes this.
  3. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    thanks, it worked with the if
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,116
    as a general rule of thumb, don't do these things with an if.
    TryParse exists for a reason. you shouldn't play smart with such things, as it'll blow up in your face eventually.
    in this particular case doing "34.34.45" or "t348" would still blow up, but hey, I'm not judging