edve98
1
I need a script that would print what button was just pressed, Once. With my script I get a message with nothing every frame (I guess) and with message with my button (when I press it). How can I avoid this?
here is my super simplified script:
function OnMouseDown () {
while (true) {
if(Input.GetKey){
print(Input.inputString);
}
yield;
}
}
Solved:
function Update () {
if(Input.anyKeyDown){
print(Input.inputString);
}
}
First: OnMouseDown is only called the first frame when you press the mouse button. So unless you can time yourself by pressing the mouse button AND a key within the same frame, OnMouseDown will not work.
Second: Input.GetKey always returns true even if no key was pressed. Input.GetKey(“s”) will only return true when the user presses “s”. Instead use Input.anyKeyDown. this checks for any key being pressed AND only prints the key for the first frame the key was pressed - not every frame the key is pressed.