Array od KeyCode

I create script, where Player can Press Key(0-9) and chose Weapon. Is there any way to do this conditions in one loop using Array?

            if (Input.GetKey(KeyCode.Alpha1))
            {

            } /// Alpha1 Alpha2...   ...Alpha9
            else if (Input.GetKey(KeyCode.Alpha9))
            {

            }

Something like that:

        for (int i=0; i< maxWeapons; i++)
        {
            if (Input.GetKey(KeyCode.Array[i]))
            {

            } /// Alpha1 Alpha2...   ...Alpha9
            else if (Input.GetKey(KeyCode.Array[i]))
            {

            }
        }

But I don’t know what Type Array I need here
Thx for answers :slight_smile:

I believe you’re on the right track. Try something like this.

private int[] weaponSelections = new int[] {0,1,2,3,4,5,6,7,8,9};

public void Update() {
    if (Input.anyKeyDown) {
        for (int i = 0; i < weaponSelections.Length; i += 1) {
            if (Input.GetKeyDown(i.ToString())) {
                // Switch weapon code here
                ChangeToWeapon(i);
            }
        }
    }
}
1 Like

Thx you for answer. I was thinking about something like that, but In this case I need to set Name in Projects Settings-> Input, It isn’t a problem, but I wonder, if it’s possible to do it with using KeyCode.Array

Ah, yes, that’s planning ahead. Hmm…