Did you try Parsing directly the input value to INT?

Hi There,
I have figured out how to Parse the string value in an integer that will be stored in “int level” variable.
Maybe it is an overkill but I think it is well worth trying it.

     void OnUserInput(string input)
        {
            if (input == "1"|| input == "2" || input == "3")
            {
                level = System.Int32.Parse(input);
                StartGame();
            }
            else
            {
                print("bad value");
                ShowMainMenu();
            }

        }

Hi,

Take a look at TryParse also, this can give you the conversion but also a return value to indicate whether the parse was successful or not, so, if you had made a mistake an let a word through, for example, it would return false which you could then use in validation. Parse will give the conversion if it can, but throw an exception if it can’t.


See also;

2 Likes

Privacy & Terms