Hi, I’ve been having some trouble getting keys from playerprefs. I have successfully set the keys in forms of strings but i can’t seem to get them to detect if they are being pushed or not.
For setting my keys i’m using a Dictionary.
Dictionary<string, KeyCode> keys;
When I’m pulling from my playerprefs to add them to a dictionary they work just fine.
keys.Add("Attack", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Attack", "Q")));
However, When i try to use them in a:
if (Input.GetKey((KeyCode)System.Enum.Parse(typeof (KeyCode), PlayerPrefs.GetString("Attack"))))
It doesn’t seem to detect my input. if I change my input using my keybinds manager/keybinds screen.
public void SaveKeys() {
foreach (var key in keys) {
PlayerPrefs.SetString(key.Key, key.Value.ToString());
}
}
It still doesn’t seem to want to detect my keys no matter which keys i have put into the PlayerPrefs.
Help! D: idk what i’m doing wrong :((((
P.S. Here is my full script. I wanted to put it at the end so I can hit my key points first.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class Keybinds : MonoBehaviour
{
private Dictionary<string, KeyCode> keys = new Dictionary<string, KeyCode>();
public GameObject[] labels;
private GameObject currentKey;
private bool isKeysMenu;
// Use this for initialization
void Start()
{
isKeysMenu = false;
keys.Add("Attack", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Attack", "Q")));
keys.Add("Action", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Action", "E")));
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", "Space")));
displayKeys();
}
public void displayKeys() {
for (int i = 0; i < keys.Count; i++)
{
labels[i].transform.GetChild(0).GetComponent<Text>().text = keys[labels[i].gameObject.name].ToString();
}
}
// Update is called once per frame
public void isKeybindsMenu(bool b) {
if (!b) {
isKeysMenu = false;
}
else {
isKeysMenu = true;
}
}
void OnGUI() {
if (isKeysMenu) {
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();
Debug.Log(currentKey.name);
currentKey = null;
}
}
}
}
public void changeKey(GameObject clicked) {
currentKey = clicked;
}
public void SaveKeys() {
foreach (var key in keys) {
PlayerPrefs.SetString(key.Key, key.Value.ToString());
}
}
public Dictionary<string, KeyCode> getKeys() {
return keys;
}
}