- Home /
Custom keybinding stopped working for one of the keybinding options
Hello. My keybinding / key mapping stopped working for one of the options, although the code is completely identical to the rest of the keybinding options.
Basically, you press a button with the mouse and then assign the key with the keyboard, and that works fine for "left", "right", "jump" and "shoot". But somehow with the "Up" key it defaults to UpArrow. And here is the problem: It defaults to a key, and then its like its stuck, cause when i press save it changes the other buttons to whatever i pressed them to be, but this one is stuck and keeps defaulting to UpArrow.
I tried to find the playerprefs file and delete it, but somehow i can't find it. I also tried to do PlayerPrefs.DeleteAll() but that doesnt help either. Also tried to make a new scene and redo it all, but nothing works, also tried upgrading unity to see if it fixed it as a last resort, but nope.
What do i do? Does anyone have any suggestions to what i can do? please help. Thanks.
p.s it worked fine in a previous build of the game, but i changed something and i cant remember what. so that kinda sucks
Heres the code:
public class KeyMapping : MonoBehaviour
{
public bool dontDestroyOnLoad = false;
public static KeyMapping Instance;
public Dictionary<string, KeyCode> keys = new Dictionary<string, KeyCode>();
public Text up, left, right, jump, shoot;
public GameObject currentKey;
void OnEnable()
{
if (dontDestroyOnLoad)
{
DontDestroyOnLoad(this);
}
LoadStuff();
}
void Awake()
{
Instance = this;
}
void Start()
{
keys.Add( "Left", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Left","A")));
keys.Add( "Right", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Right", "D")));
keys.Add( "Jump", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Jump", "X")));
keys.Add( "Shoot", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Shoot", "C")));
keys.Add( "Up", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Up", "T")));
up.text = keys["Up"].ToString();
left.text = keys["Left"].ToString();
right.text = keys["Right"].ToString();
jump.text = keys["Jump"].ToString();
shoot.text = keys["Shoot"].ToString();
}
void OnGUI()
{
if(currentKey != null)
{
Event e = Event.current;
if(e.isKey)
{
keys[currentKey.name] = e.keyCode;
currentKey.transform.GetChild(0).GetComponent<Text>().text = e.keyCode.ToString();
currentKey = null;
}
}
}
public void GoBackToMainMenu()
{
SceneManager.LoadScene("MainMenu");
}
public void ChangeKey(GameObject clicked)
{
currentKey = clicked;
}
public void SaveStuff()
{
foreach(var key in keys)
{
PlayerPrefs.SetString(key.Key, key.Value.ToString());
}
print(keys["Shoot"]);
print(keys["Up"]);
PlayerPrefs.Save();
}
public void LoadStuff()
{
foreach (var key in keys)
{
PlayerPrefs.GetString(key.Key, key.Value.ToString());
}
}
}
Your answer
Follow this Question
Related Questions
Key not recognized 2 Answers
Is it possible to change keyboard input inGame 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Missed key input 1 Answer