Better way to get Action Input buttons

During the lesson, Sam mentioned that he didn’t know a better way to connect the keycodes to the action indexes.

I am sending here a possible solution. Please, be aware that this will require you to configure the slots accordingly to your UI.

var firstSlot = KeyCode.Alpha1;
var lastSlot = KeyCode.Alpha6;
for (var i = firstSlot; i <= lastSlot; i++)
{
    if (Input.GetKeyDown(i))
    {
        actionStore.Use(i - firstSlot, gameObject);
    }
}

Using the button 0 is a special edge case, as its keycode is smaller than Alpha1. Based on that, if you want to use the button zero, I recommend add it as a separate action outside the for loop.

Hope this helps someone :slight_smile:

2 Likes

Thank you for this! This will help students out.

1 Like

Privacy & Terms