- Home /
How do I specify a public keyboard-character variable in a script?
Hello,
I want to specify a public key press variable to use within my script, something like:
var myKeyChar : char = 'v'; function Update () {
if(Input.GetKeyDown(myKeyChar)
{
...
}
}
But the public myKeyChar doesn't show up in the editor? I'm new to .js (coming from C++), so maybe I'm approaching this the wrong way.
Thanks
FourSheds
Answer by Mike 3 · Jul 16, 2010 at 10:26 AM
Use String instead of char (GetKeyDown takes a string anyway)
Also - I don't think you can even use character literals in javascript. The usual hack is to use "v"[0]
Alternatively, you could add var keyCode : KeyCode = KeyCode.V;
That should let you pick a key from a dropdown list, and be type safe
Thanks for the quick answer $$anonymous$$ike, works perfectly now :)
Edited my answer to give you another method, which is probably preferable
Useful trick, I wonder if there's some way to list the input mappings (Fire1, Fire2 etc.) in a similar way. I guess not though as they appear to be just strings.
Your answer
Follow this Question
Related Questions
Getting a variable from another script 2 Answers
Getting a variable from another object 1 Answer
Converting 'char' to the name of a variable 1 Answer
Scriptable tile 1 Answer
Public Texture Variables Not Working 0 Answers