- Home /
Please Help. Tryna make a script that binds controls to random keys. but i cannot find a way to do it quickly and can only set up each key manualy
my plan was to have a char array of all 26 letters of the alphabet and then randomly pick 1 of them to set as a new bind for a control, but i cannot find a way to turn a string into code, to then execute a BindForLeft = keycode.Letter (it would just be a capital letter of the alphabet).
is there any way to turn string into code? i dont want to have to write an if statement for every letter, plus i might also add other language letters so it would be very time consuming.
Comment
Best Answer
Answer by Hellium · Dec 14, 2021 at 06:27 PM
The letter keycodes from A to Z are following
CODE NOT TESTED
int count = (int)(1 + 'z' - 'a');
List<UnityEngine.KeyCode> keycodes = new List<KeyCode>(count);
for (int i = 0; i < count; ++i)
keycodes.Add(KeyCode.A + i);
int bindingIndex;
bindingIndex = Random.Range(0, keycodes.Count);
BindForLeft = keycodes[bindingIndex];
keycodes.RemoveAt(bindingIndex);
bindingIndex = Random.Range(0, keycodes.Count);
BindForRight = keycodes[bindingIndex];
keycodes.RemoveAt(bindingIndex);
Your answer
![](https://koobas.hobune.stream/wayback/20220613050551im_/https://answers.unity.com/themes/thub/images/avi.jpg)