Unity: Assigning a key to perform an action in the inspector

Posted by Marc Pilgaard on Game Development See other posts from Game Development or by Marc Pilgaard
Published on 2011-11-22T13:03:19Z Indexed on 2011/11/23 2:10 UTC
Read the original article Hit count: 392

Filed under:
|
|

I am trying to write a simple piece of code in JavaScript where a button toggles the activation of a shield, by dragging a prefab with Resources.load("ActivateShieldPreFab") and destroying it again (Haven't implemented that yet).

I wish to assign this button through the inspector, so I have created a string variable which appears as intended in the inspector. Though it doesn't seem to register the inspector input, even though I changed the value through the inspector.

It only provides the error: "Input Key named: is unknown" When the button name is assigned within the code, there is no issues.

Code as follows:

var ShieldOn = false;
var stringbutton : String;

function Start(){
}

function Update () {
    if(Input.GetKey(stringbutton) && ShieldOn != true)
    {
        Instantiate(Resources.load("ActivateShieldPreFab"), Vector3 (0, 0, 0), Quaternion.identity);
        ShieldOn = true;
    }  
}

© Game Development or respective owner

Related posts about unity

Related posts about JavaScript