- Home /
Convert String to a type
Hello, is there a way to make a string into a type? for example:
var aKey : String;
aKey = "C";
if(Input.GetKey(KeyCode.aKey))
{
Debug.Log("Holding C");
}
This returns the error 'aKey' is not a member of 'UnityEngine.KeyCode'. Is there a way to change its type? Or maybe a better way of writing this. Thanks
If all you want to do, is edit the key in the inspector. then do "var a$$anonymous$$ey:$$anonymous$$eycode; "
Answer by bzgeb · Sep 20, 2012 at 10:25 PM
Try changing your code to:
var aKey : String;
aKey = "C";
if(Input.GetKey(aKey))
{
Debug.Log("Holding C");
}
Exactly, Get$$anonymous$$ey takes either a $$anonymous$$eyCode value or a string value which holds the name of a key. I'm not sure about case-sensitivity, but just try it out ;)
An alternative would be to use the System.Enum class to convert the string into an enum value, but don't ask me for the syntax in UnityScript ;) I use C# only.
It should be something like that in UnityScript (not tested):
var a$$anonymous$$ey : String;
a$$anonymous$$ey = "C";
var keycode : $$anonymous$$eyCode = Enum.Parse($$anonymous$$eyCode, a$$anonymous$$ey);
if(Input.Get$$anonymous$$ey(keycode))
Your answer
Follow this Question
Related Questions
Mecanim Animation Parameter Types: Boolean vs. Trigger 3 Answers
Passing a Script Name to a Function 2 Answers
Mecanim. Change state under parameter condition problem. 1 Answer
Key down, to edit property 1 Answer
Open scene by typing a word 2 Answers