Store and retrieve KeyCode in a List of KeyCode variables

Hi,

I am stuck on this thing and cannot quite figure a workaround.

I have a struct:

public class Keys
    {
        public string keyName;
        public KeyCode keyCode;
        public KeyCode keyCodeName;

        public Keys(string keyName, KeyCode keyCode, KeyCode keyCodeName)
        {
            this.keyName = keyName;
            this.keyCode = keyCode;
            this.keyCodeName = keyCodeName;
        }
    }

I have a list of variables:

public static KeyCode UserKeyUpPrimary;
public static KeyCode UserKeyDownPrimary;
public static KeyCode UserKeyLeftPrimary;
public static KeyCode UserKeyRightPrimary;

I have a list of these variables:

public List<Keys> keys = new List<Keys>();

keys.Add(new Keys("Up", KeyCode.W, UserKeyUpPrimary));
keys.Add(new Keys("Down", KeyCode.X, UserKeyDownPrimary));
keys.Add(new Keys("Left", KeyCode.A, UserKeyLeftPrimary));
keys.Add(new Keys("Right", KeyCode.D, UserKeyRightPrimary));

When I try to write to the variable inside the list like this, it doesn’t work:

//Assign the key bind to the variable
keys[i].keyCodeName = (KeyCode)PlayerPrefs.GetInt("KeyBind" + keys[i].keyName);

When I write like this, it does work:

//Assign the key bind to the variable
UserKeyUpPrimary = (KeyCode)PlayerPrefs.GetInt("KeyBind" + keys[i].keyName);

How can I call the variable KeyCode inside the list and assign a KeyCode to it without calling it by name?

Thank you

before I try and answer, are you trying to produce a system for allowing a user to modify the keys in which control the game?

Hi,

Yes, I have already created it and it all works, but it’s bugging me I had to address the variable directly.

From your question, I feel like you have something up your sleeve that may be a better solution to what I’ve done altogether.