- Home /
String as a input
I'm trying to make a public variable "button" and later use it as a input key. for testing purposes, I would like to be able to change string in the inspector that I want to press as the input. I'/m pretty new to scripting, and trying to learn some c#. I tried something like this:
public string button = "";
void Update () {
if (Input.GetKeyDown (KeyCode.button)) {
resetCubes();
}
}
that is not working at all, unity says that there is no definition "button" for a keyCode
Answer by getyour411 · May 18, 2014 at 12:30 AM
You can't do that. You can look at the the doc for valid keyCode but you can't make it dynamic in this way.
Answer by Nezabyte · May 18, 2014 at 12:33 AM
Instead of string, it should be of type KeyCode. For example:
public KeyCode button = KeyCode.W;
//...(somewhere else in your code, you can change button..)
button = KeyCode.L;
Your answer
![](https://koobas.hobune.stream/wayback/20220613145303im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Key not recognized 2 Answers
C# Script Simulating Input.inputString 0 Answers
Last key pressed 1 Answer
Issue With Lists and Keycodes 1 Answer