Hey all, I created a keybind script for my game that is intended to function as follows: Upon opening the settings/controls menu, there are buttons with text for all of my actions that are set to default keybinds. The player should be able to click on a button, press a key, and the key pressed will display on the button text. If the key is already in use by another action, that action’s button text should change to “Unassigned” and the new action should inherit the key.
I’m running into two major issues when I build and play using my current script:
Issue 1: Load into game and see default control settings indicated in my script. Change “Move Forward” action to “F” key (“Move Forward” action now changes from “W” key to “F” key – correct). Now say I want to bind the “W” key to the “Jump Action” right after changing the “Move Forward” action. Doing this causes “Move Forward” action to display “Unbound” even after I had changed it to “F” right before. It appears that if I assign a keystroke of a starting UI text objects keycode, it reverts to “Unbound” even if it was changed by me before.
Issue 2: Load into game and see default control settings indicated in my script. Change “Move Forward” action to “R” key for example (not previously assigned in my script’s dictionary). I can then change every other action in the menu to the “R” key and none of them will revert back to “Unassigned” (now I have a full menu of actions assigned to the “R” key).
What is wrong with the script and how would I go about fixing it?
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class KeyBindScript : MonoBehaviour {
private Dictionary<string, KeyCode> Keys = new Dictionary<string, KeyCode> ();
private Dictionary<string, Text> ButTextLookup = new Dictionary<string, Text> ();
private GameObject currentKey;
public Text forward, backwards, movleft, movright, leanleft, leanright, run, jump, crouch, interact, attack, clairvoyance, journal, inventory, tasks, watch, quickwheel, pausemenu;
void Start ()
{
Keys.Add("Forward", KeyCode.W);
Keys.Add("Backwards", KeyCode.S);
Keys.Add("MovLeft", KeyCode.A);
Keys.Add("MovRight", KeyCode.D);
Keys.Add("LeanLeft", KeyCode.Q);
Keys.Add("LeanRight", KeyCode.E);
Keys.Add("Run", KeyCode.LeftShift);
Keys.Add("Jump", KeyCode.Space);
Keys.Add("Crouch", KeyCode.LeftControl);
Keys.Add("Interact", KeyCode.F);
Keys.Add("Attack", KeyCode.Mouse0);
Keys.Add("Clairvoyance", KeyCode.Mouse1);
Keys.Add("Journal", KeyCode.J);
Keys.Add("Inventory", KeyCode.I);
Keys.Add("Tasks", KeyCode.X);
Keys.Add("Watch", KeyCode.C);
Keys.Add("QuickWheel", KeyCode.Mouse3);
Keys.Add("PauseMenu", KeyCode.Escape);
Keys.Add("Additional1", KeyCode.R);
ButTextLookup.Add ("Forward", forward);
ButTextLookup.Add("Backwards", backwards);
ButTextLookup.Add ("MovLeft", movleft);
ButTextLookup.Add ("MovRight", movright);
ButTextLookup.Add ("LeanLeft", leanleft);
ButTextLookup.Add ("LeanRight", leanright);
ButTextLookup.Add ("Run", run);
ButTextLookup.Add ("Jump", jump);
ButTextLookup.Add ("Crouch", crouch);
ButTextLookup.Add ("Interact", interact);
ButTextLookup.Add ("Attack", attack);
ButTextLookup.Add ("Clairvoyance", clairvoyance);
ButTextLookup.Add ("Journal", journal);
ButTextLookup.Add ("Inventory", inventory);
ButTextLookup.Add ("Tasks", tasks);
ButTextLookup.Add ("Watch", watch);
ButTextLookup.Add ("QuickWheel", quickwheel);
ButTextLookup.Add ("PauseMenu", pausemenu);
forward.text = Keys ["Forward"].ToString ();
backwards.text = Keys ["Backwards"].ToString ();
movleft.text = Keys ["MovLeft"].ToString ();
movright.text = Keys ["MovRight"].ToString ();
leanleft.text = Keys ["LeanLeft"].ToString ();
leanright.text = Keys ["LeanRight"].ToString ();
run.text = Keys ["Run"].ToString ();
jump.text = Keys ["Jump"].ToString ();
crouch.text = Keys ["Crouch"].ToString ();
interact.text = Keys ["Interact"].ToString ();
attack.text = Keys ["Attack"].ToString ();
clairvoyance.text = Keys ["Clairvoyance"].ToString ();
journal.text = Keys ["Journal"].ToString ();
inventory.text = Keys ["Inventory"].ToString ();
tasks.text = Keys ["Tasks"].ToString ();
watch.text = Keys ["Watch"].ToString ();
quickwheel.text = Keys ["QuickWheel"].ToString ();
pausemenu.text = Keys ["PauseMenu"].ToString ();
}
void Update ()
{
if (Input.GetKeyDown(Keys["Forward"]))
{
//Move Forward
}
if (Input.GetKeyDown(Keys["Backwards"]))
{
//Move Backwards
}
if (Input.GetKeyDown(Keys["MovLeft"]))
{
//Move Left
}
if (Input.GetKeyDown(Keys["MovRight"]))
{
//Move Right
}
if (Input.GetKeyDown(Keys["LeanLeft"]))
{
//Lean Left
}
if (Input.GetKeyDown(Keys["LeanRight"]))
{
//Lean Right
}
if (Input.GetKeyDown(Keys["Run"]))
{
//Run
}
if (Input.GetKeyDown(Keys["Jump"]))
{
//Jump
}
if (Input.GetKeyDown(Keys["Crouch"]))
{
//Crouch
}
if (Input.GetKeyDown(Keys["Interact"]))
{
//Interact With Object
}
if (Input.GetKeyDown(Keys["Attack"]))
{
//Attack
}
if (Input.GetKeyDown(Keys["Clairvoyance"]))
{
//Initialize Clairvoyance
}
if (Input.GetKeyDown(Keys["Journal"]))
{
//Open Journal
}
if (Input.GetKeyDown(Keys["Inventory"]))
{
//Open Inventory
}
if (Input.GetKeyDown(Keys["Tasks"]))
{
//Open Task Menu
}
if (Input.GetKeyDown(Keys["Watch"]))
{
//Look At Watch
}
if (Input.GetKeyDown(Keys["QuickWheel"]))
{
//Open Quick Wheel
}
if (Input.GetKeyDown(Keys["PauseMenu"]))
{
//Open Pause Menu
}
}
void OnGUI()
{
if (currentKey != null) {
Event e = Event.current;
if (e.isKey) {
Keys [currentKey.name] = e.keyCode;
foreach (var kvp in Keys.ToList()) {
if (kvp.Value == e.keyCode) {
if (currentKey.name != kvp.Key) {
Keys [kvp.Key] = KeyCode.None;
ButTextLookup [kvp.Key].text = "*Unbound*";
}
}
}
currentKey.transform.GetChild(0).GetComponent<Text> ().text = e.keyCode.ToString ();
currentKey = null;
}
}
}
public void ChangeKey(GameObject clicked)
{
currentKey = clicked;
}
}