Checking if Keyboard input was an integer
Hey guys, I've been trying to find a way to detect if someone inputs an integer from the keyboard or not.
Basically I want the console to only print the integers 0-9 corresponding to the "Alpha" keys.
So far I have gotten to here:
string userInput;
bool userValue;
void Update () {
if (Input.anyKeyDown) {
OnGUI ();
}
}
void OnGUI () {
Event e = Event.current;
if (e.isKey){
string key = e.keyCode.ToString();
Debug.Log(key);
}
}
Now I realize you aren't technically supposed to use OnGUI but I have tried literally every other way and the console will print empty strings. Don't know why and no one on the forums really had an answer either.
This is what I have tried to convert the string into an integer and print it to console:
string userInput;
int userNumber;
bool res = true;
void IntegerInput (){
userInput = Input.inputString;
bool res = int.TryParse(userInput, out userNumber);
Debug.Log(res);
Debug.Log(userInput);
Debug.Log(userNumber);
}
However it never prints anything when I put it under the anyKeyDown in my if statement under the Update().
If anyone knows a better way to go about this or has some guidance that would be great. I've spent a good number of hours just trying to do this because I am trying to learn how to optimize code rather than rely on tons of if statements to get what I want done.
In the end I am trying to get the user to choose a number by typing it out and if they try to type anything other than an integer the console will warn them that they must give positive integers.
Your answer
Follow this Question
Related Questions
Player animation FSM stopped working 0 Answers
why StartCoroutine(SpawnBigTree()); don't work and the float has a error 0 Answers
Word guessing game 1 Answer
AnimationComplete() Error 0 Answers