0
\$\begingroup\$

I am trying to create multiple selections for some items on my UI. To perform multiple selections of items I want a user to hold left control button. But then I add

if (Input.GetKeyDown(KeyCode.LeftControl)){
/* Logic */
}

It registers in debugger what LeftControl is pressed but it skips all logic in brackets. This function is public and created for UI button. Is Input and Button.OnClick compatible at all?

\$\endgroup\$
  • 1
    \$\begingroup\$ Try Input.GetKey(KeyCode.LeftControl) : will return true every frame while the button is pressed, allowing you to detect the OnClick event. Otherwise, you need to press the key the same frame you click on the button (quite hard) \$\endgroup\$ – Hellium Jun 12 '18 at 8:22
  • \$\begingroup\$ This solution helped. You can make an answer I will accept it. \$\endgroup\$ – Random generalist Jun 12 '18 at 9:04
2
\$\begingroup\$

Answer extracted from comment

With your current code, your user needs to press the key the same frame he / she clicks on the UI button, which is quite hard.

Instead, you should simply check if the key is currently pressed by using Input.GetKey. This function returns true while the user holds down the key:

if (Input.GetKey(KeyCode.LeftControl))
{
   /* Logic */
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.