- Home /
Why does this not work? Trying to use Keycode in variable.
This script isn't working because it says that the Input.GetButtonDown doesn't take a string ,but I'm inserting a Keycode variable. Here's the script:
//SAMPLE OF THE CODE
public KeyCode onOffButton = KeyCode.Tab;
private bool inventoryOn;
void InventoryOnAndOff()
{
if (Input.GetButtonDown (onOffButton))
{
inventoryOn = !inventoryOn;
}
}
:) Next time read the error more carefully. Input.GetButtonDown only takes a string, so you read the error the other way round.
Answer by oasisunknown · Jun 10, 2014 at 05:36 AM
I did some testing on this and here is what I came up with.
1) set up your keykode in the input manager under Edit>project settings>Input
in the input manager you can name the input and set the keycode to what ever you like for example some of the other ones are "Fire1" ect. (for my testing purposes I named it "Inventory")
setting it up through the input manager allows you to change it easily and also let your player map any key to it that they like. (provided you give them access to it.)
2) how you would change it in your script after setting up the input manager would be as follows.
if (Input.GetButtonDown ("Inventory"))// this calls the input named inventory from the input manager
3) the last thing that you have to do is call your function from the Update method.
because right now you dont have one so your function wont do anything until called anyways.
ie.
void Update ()
{
InventoryOnAndOff();
}
Yes, I know. I was only giving a sample of the script that I had. Thanks for the answer!
Your answer
![](https://koobas.hobune.stream/wayback/20220613151212im_/https://answers.unity.com/themes/thub/images/avi.jpg)