I have been trying to search for something that will allow me to store what has been pressed by the user as a variable.
I realize that this could be “solved” by making if statements for every key possible but that seems inefficient.
I’ve tried several types of code but right now this is what I am sticking with:
Method #1
void OnGUI() {
Event e = Event.current;
if (e.isKey){
string key = e.keyCode.ToString();
Debug.Log(key);
print ("Hi");
}
}
Method #2
userInput = Input.inputString;
print (userInput);
Method #3
foreach (char c in Input.inputString) {
if (c == "\b"[0]) {
print (Input.inputString);
}else{
if (c == "\n"[0] || c == "\r"[0]) {
print (Input.inputString);
}else{
print (Input.inputString);
}
}
}
#1: It should work (and there are several other version I have tried) but each time the console puts a warning of “Unreachable code” and doesn’t print anything.
#2: This just prints an empty string and gives the above warning code.
#3: Same as #1.
Anyone have any idea how to fix this? I’m truly lost as all the answers on the internet are exactly the same as my solution saying that it worked for them but it does not for me.
All I need is for the console to print what was pressed.
Option 1 is what I’ve gone with in the past, and it hasn’t given me any trouble. As far as I can see, there is no unreachable code. But you could check by debugging Event.current.isKey and making sure that it is changing when a key is pressed.
We cannot help you remove the null reference if you do not show us the code.
But either way it simply means you told it to do something and it could not find what it was looking for.
(Im no pro, just someone trying to help.)
The only thing I’m trying to figure out now is how to make it so it only prints integer inputs ie: 0-9
I’ve tried this:
string userInput
int userNumber;
bool valueIsInt;
if(int.TryParse(userInput, out userNumber)) {
valueIsInt = true;
Debug.Log ("String is the number: " + userNumber);
}
But I don’t think I am using this correctly. Basically trying to test if the string can be parsed into an int and if it can set a bool to true and print the number.