Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;
    }  
}
share|improve this question
1  
When you do Debug.Log(stringButton), what does it say? Does that text appear in the list of valid button names here: unity3d.com/support/documentation/Manual/Input.html ? I also suspect you should be using GetKeyDown instead of GetKey. –  Kylotan Nov 22 '11 at 14:03
    
From the console it gives this output: UnityException: Input Key named: is unknown UnityEngine.Input.GetKey (System.String name) (at C:/BuildAgent/work/842f9557127e852/Runtime/ExportGenerated/Editor/UnityEngineInp‌​ut.cs:147) ActivateShield.Update () (at Assets/ActivateShield.js:8) –  Marc Pilgaard Nov 22 '11 at 14:11
    
You've not hit the line with the Debug.Log call on it. You need to execute that before the exception is raised, by putting the Log call before the GetKey call that is raising the exception. Basically, you're going to raise a lot of exceptions when programming in Unity, so you need to get adept at putting logging in to find out what is going on. –  Kylotan Nov 23 '11 at 0:00

4 Answers 4

up vote 1 down vote accepted

For at least one object in your scene, the variable stringbutton isn't assigned. The error "Input Key named: is unknown" shows that it's not displaying a key's name (otherwise the error would be more like "Input Key named: up is unknown").

For every instance of whatever object has that script on it, you need to ensure it has a legitimate value in the inspector. The value additionally must match the name of a key as expected by the Input Manager (see Input.GetKey()'s documentation for further details) or you'll just get different errors. If you have instances that don't need to check that button, either remove that script from the object or check that stringbutton has a value before checking Input.GetKey().

share|improve this answer
    
Input.GetKey actually corresponds to key strings, not aliases set up in the input manager. If you want input manager strings you should use GetButton. –  Tetrad Nov 22 '11 at 20:07
    
Right. I've edited my answer to (hopefully) better portray that. –  chaosTechnician Nov 22 '11 at 21:58

Hm, you've probably solved this by now, but I'll post this here in case anybody else gets stuck with this.

You just needed your var to be:

var button : KeyCode;

This will expose a drop down for which key you want in the editor. The rest of your code is correct, so this would be right:

if(Input.GetKey(button) && ShieldOn != true)
share|improve this answer

You're using the wrong pattern.

If you want to change the key, you should use Input.GetButton. http://unity3d.com/support/documentation/ScriptReference/Input.GetButton.html

Give the key a name, and then you can assign the key you want to use from the input manager. End users can then also assign that key from the launcher (assuming a standalone build).

share|improve this answer

Okay thanks for the help everybody but I think I found the solution myself, though I am not hundred percent sure of what I did to resolve it.

After playing around with it for some time, I decided to put it away and return to it later. When I returned, I cleared the entire scene, and began my scene on a fresh, making sure no other GameObjects where using the script for activating the shield.

I did a little change within the code, though I am pretty sure that didn't change the outcome:

var ShieldOn = false;
var stringbutton : String;
var prefab : GameObject;

function Start(){
}

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

As you can see, I didn't do any changes with the GetKey or GetButton, but it still works as intended, and now you can assign which button you wish to use through the inspector.

share|improve this answer
    
If you changed nothing in the script, removed objects, and carefully re-added them to ensure your script was only on the appropriate objects, I suspect the problem was that you had the script on an object you didn't intend. –  chaosTechnician Nov 24 '11 at 0:52
    
Yeah, it was probably the issue. I can't come to think of anything that could have caused the error. –  Marc Pilgaard Nov 24 '11 at 9:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.