- Home /
input key named : KeyCode.Escape is unkown
when a try to test my game it says "input key named : KeyCode.Escape is unkown" and i don't know why.
this is the code i used.
var GUIEnabled : boolean = false;
var autoSave : boolean = false;
var paused : boolean = false;
function Start() {
OnLoad();
if(autoSave == true) {
autoSaveEnable();
}
}
function Update () {
if(Input.GetKey("KeyCode.Escape") ) {
GUIEnabled = !GUIEnabled;
}
if(Input.GetKey("KeyCode.Escape") && paused == false){
paused = true;
Time.timeScale = 0;
}
else if(Input.GetKey("KeyCode.Escape") && paused == true){
paused = false;
Time.timeScale = 1;
}
if(Input.GetKeyDown("p") && paused == false && GUIEnabled == false){
paused = true;
Time.timeScale = 0;
}
else if(Input.GetKeyDown("p") && paused == false && GUIEnabled == true) {
paused = true;
Time.timeScale = 0;
}
else if(Input.GetKeyDown("p") && paused == true && GUIEnabled == false) {
paused = false;
Time.timeScale = 1;
}
else if(Input.GetKeyDown("p") && paused == true && GUIEnabled == true) {
paused = true;
Time.timeScale = 0;
}
if(autoSave == true) {
autoSaveEnable();
}
}
function OnGUI () {
if(GUIEnabled) {
if (GUI.Button (Rect (Screen.width / 2,Screen.height / 2 - 40,105,20), "Save Data")) {
OnSave();
}
if (GUI.Button (Rect (Screen.width / 2,Screen.height / 2 - 20,105,20), "Autosave True")) {
autoSave = true;
}
if (GUI.Button (Rect (Screen.width / 2,Screen.height / 2 - 0,105,20), "Autosave False")) {
autoSave = false;
}
if (GUI.Button (Rect (Screen.width / 2,Screen.height / 2 - -20,105,20), "Exit")) {
GUIEnabled = !GUIEnabled;
paused = false;
Time.timeScale = 1;
}
}
}
function autoSaveEnable() {
for(var x = 1; x>0; x++) {
yield WaitForSeconds(1);
Debug.Log("save me");
OnSave();
}
}
// a function created to save a game
function OnSave(){
PlayerPrefs.SetInt("CurXp", playerStats.curXp);
PlayerPrefs.SetInt("MaxXp", playerStats.maxXp);
PlayerPrefs.SetInt("CurHp", playerStats.curHp);
PlayerPrefs.SetInt("MaxHp", playerStats.maxHp);
PlayerPrefs.SetInt("CurMana", playerStats.curMana);
PlayerPrefs.SetInt("MaxMana", playerStats.maxMana);
PlayerPrefs.SetInt("Level", playerStats.level);
}
// a function created to load a game
function OnLoad(){
Debug.Log("loaded");
playerStats.curXp = PlayerPrefs.GetInt("CurXp");
playerStats.maxXp = PlayerPrefs.GetInt("MaxXp");
playerStats.curHp = PlayerPrefs.GetInt("CurHp");
playerStats.maxHp = PlayerPrefs.GetInt("MaxHp");
playerStats.curMana = PlayerPrefs.GetInt("CurMana");
playerStats.maxMana = PlayerPrefs.GetInt("MaxMana");
playerStats.level = PlayerPrefs.GetInt("Level");
}
Please use meaningful titles and not "why doesn't this code work?" Also format code as code by using the 101/010 button. I edited that for you.
Answer by tanoshimi · Jan 04, 2014 at 02:45 PM
Because you're passing it as if it's a string.
Use:
Input.GetKey(KeyCode.Escape)
Not:
Input.GetKey("KeyCode.Escape")
Get$$anonymous$$ey can be passed a string as well, though it would have to be in a different format - You wouldn't write "$$anonymous$$eyCode.X" but just "X", according to the 'button names' section on the input documentation:
docs.unity3d.com/Documentation/$$anonymous$$anual/Input.html
So it would be written as:
Input.Get$$anonymous$$ey("escape")
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
itween move to do not end(even it is) 0 Answers
Fps Aiming Script help 2 Answers
Can I use DataTable in unity and how? 1 Answer
Stop Script Completely. Doesn't Execute Another Line Of Code. 1 Answer