Search Unity

if (variable != a-z)

Discussion in 'Scripting' started by sevisin, Jan 24, 2011.

  1. sevisin

    sevisin

    Joined:
    Dec 26, 2010
    Posts:
    145
    Hello,

    I have a small problem.

    I'm practicing on GUIs and I'm trying to have the user input a number for an array size. GUI.TextField requires a string and that is, from what I saw, the only real user input able text field for unity GUIs. I converted the string into an int and used an "for-in" conditional statement to do what i want with the array. In addition, I used an "if" conditional statement to see if the input is of numeric value.

    if (userInput != "")

    However, the user is still able to type in letters in the field and it gives me an error in the console.

    I'm having a total brain fart on the issue, but I'm trying to think of a way to make a conditional statement to test to see if the input is a valid int with no letters.

    Any thoughts?

    -S
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Prevent any non-numeric characters from being used:

    Code (csharp):
    1. if (Event.current.character < "0"[0] || Event.current.character > "9"[0]) {
    2.     Event.current.character = "\0"[0];
    3. }
    4. userInput = GUI.TextField(...
    --Eric
     
  3. sevisin

    sevisin

    Joined:
    Dec 26, 2010
    Posts:
    145
    @Eric5h5
    Thanks, I'll give that a shot.
    -S