Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Input text converted into int array each time I hit Enter. "If error (letters instead of num)" code?

Discussion in 'Scripting' started by NMichiotis, Jul 1, 2023.

  1. NMichiotis

    NMichiotis

    Joined:
    Feb 10, 2023
    Posts:
    2
    I have an input field where the player has to put a 4-digit number. Isn't there a way I can write a code for when this error is shown on the Unity debug log? "FormatException: Input string was not in a correct format"
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Showing your existing code would help.

    But you probably want something along the lines of
    int.TryParse
    .
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    It‘s called exception handling. You enclose the code with try/catch blocks and handle the exceptions in an appropriate way, such as reverting to the previous valid value and alerting the user that the input is invalid.
     
    NMichiotis likes this.
  4. NMichiotis

    NMichiotis

    Joined:
    Feb 10, 2023
    Posts:
    2
    omg thank you so much; worked like a charm!