2
\$\begingroup\$

I have a turn based roguelike with input issues that are driving me nuts.

I use the Unity Input/GetAxis() for keyboard input. I recently added a skill bar with clickable buttons that pass an int parameter to a simple class when clicked.

The problem I am having is that the OnClick() event, on the last button that was clicked, fires whenever the SPACE bar is pressed.

What could cause this? I do not have space set on any of the input Axes (I did originally and both the axes attached to SPACE and the OnClick() would fire)

Edit: The code is below. It is called by the OnClick() handler on the button in the inspector. If you run the project and press SPACE nothing happens. Then if you click a button the Debug.Log fires. Then press SPACE again and the debug fires again with the same button number as was last clicked.

public class SkillClickNotifier : MonoBehaviour
{
    [HideInInspector]
    public int SkillClicked;

    public void ButtonClicked(int skill)
    {
        Debug.Log("Button Clicked: "+ skill);
        // SkillClicked = skill;
        // GameManager.Notifications.PostNotification(this, "SkillClicked");
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ I should probably note that I have been able to be certain that it is not my input system as the OnClick() is firing from Unity. I'm wondering if there is a setting or something I was unable to find with Google \$\endgroup\$
    – Mike B
    Sep 20 '16 at 20:23
  • \$\begingroup\$ Post your input code. \$\endgroup\$
    – jgallant
    Sep 20 '16 at 20:41
  • \$\begingroup\$ I finally found the answer. It was the Navigation property on the button. I thought it just had to do with a Tab order but it allows for keyboard Mouse Input \$\endgroup\$
    – Mike B
    Sep 20 '16 at 21:15
  • \$\begingroup\$ Do you have a canvas with a eventsystem? \$\endgroup\$
    – Leo
    Sep 20 '16 at 21:31
  • \$\begingroup\$ Yes, all UI controls are on a Canvas with an Event System \$\endgroup\$
    – Mike B
    Sep 20 '16 at 22:18
1
\$\begingroup\$

I found the answer and am answering it myself in the hope it will help someone in the future.

In the Unity Button control there is a property called Navigation that defaults to Automatic. This is to allow keyboard navigation and was causing my issue when the Space bar was pressed. The correct setting for me was None.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ You can also remap the "Submit" button in the Input Manager so space won't be used for keyboard navigation, in case you want to retain keyboard nav for menus with enter/return as your submit action. \$\endgroup\$
    – DMGregory
    Sep 20 '16 at 22:20
  • \$\begingroup\$ OMG I went through all the inputs with a fine toothed comb and still missed the alt-positive on Submit being space... Thanks \$\endgroup\$
    – Mike B
    Sep 21 '16 at 3:04
  • \$\begingroup\$ To remap the "Submit" button in the Input Manager: Go to Edit > Project Settings > Input Manager > Submit: Set the positive and alt positive button. \$\endgroup\$
    – scho
    Oct 15 at 10:52
1
\$\begingroup\$

When you click on the button, there will then be a color surrounding it, telling the user that the button is now the selected UI item. Hitting Space at this point "activates" the UI item.

This will not necessarily solve the issue, but clicking off the screen will remove the selection, and so when you'll press Space again, the OnClick event will not fire.

\$\endgroup\$
1
  • \$\begingroup\$ You can also set the selected UI object via script, to deliberately deselect the object so that subsequent key presses don't trigger it. Just be cautious about doing this, as it could break accessibility for players who use the keyboard to navigate your UI, so ensure there's still a clear and convenient way for them to access that function. \$\endgroup\$
    – DMGregory
    Oct 19 at 12:52

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.