[英]Input not being detected in scene Unity. Works when starting the game from that scene; does not work otherwise
I'm currently working on a game where the start screen is level 0, the actual playable levels are levels 1-4, and the lose screen is level 5. My issue is with the lose screen. 我当前正在开发一个游戏,其中开始屏幕为0级,实际可玩级别为1至4级,失败屏幕为5级。我的问题是失败屏幕。 Here's my code:
这是我的代码:
void Update () {
if(Input.GetKeyDown(KeyCode.Return) && (CurrentLevel == 0)){
CurrentLevel = 1;
Application.LoadLevel (CurrentLevel);
}
if (CharacterMovement.Score == 10) {
CurrentLevel = 2;
CharacterMovement.Score = 11;
Application.LoadLevel (CurrentLevel);
}
if (CharacterMovement.Lives == 0) {
isLost = true;
Debug.Log ("is now true");
CurrentLevel = 5;
Debug.Log ("current level is set to 5");
Application.LoadLevel (CurrentLevel);
}
if (CurrentLevel == 5) {
Debug.Log ("this is level 5");
if (Input.GetKeyDown(KeyCode.Tab)) {
Debug.Log ("tab is pressed");
}
}
}
When I lose all of my lives I'm properly sent to the lose screen, and I receive the following logs from Debug.Log
: "is now true", "current level is set to 5", and "this is level 5." 当我失去一生时,我会被正确地发送到“丢失”屏幕,并且从
Debug.Log
收到以下日志:“现在为true”,“当前级别设置为5”和“这是级别5”。 “ However, when I press tab on the lose screen, I never receive the log "Tab is pressed". 但是,当我在丢失屏幕上按Tab时,我永远都不会收到“按了Tab键”的日志。 This is important because I want to be able to reset the game to the start screen when the key is pressed.
这很重要,因为我希望能够在按下该键时将游戏重置到开始屏幕。
The weird thing is that if I press play from the lose screen in the Unity editor and hit tab, it works perfectly. 奇怪的是,如果我在Unity编辑器中从失败屏幕中按播放,然后单击选项卡,则效果很好。 But if I play through again and lose all my lives and try to hit tab, it doesn't work again.
但是,如果我再次玩弄而失去了全部生命,并试图击中选项卡,它将不再起作用。
What's going on here? 这里发生了什么?
When you lose the game and load scene 5, Your Lives
still are zero, so CharacterMovement.Lives == 0
will be true and you just load scene 5 repeatedly. 当您输掉游戏并加载场景5时,您的
Lives
仍然为零,因此CharacterMovement.Lives == 0
将为true,并且您只需重复加载场景5。 If you look at your console I guess there is a lot of this is level 5
in that. 如果您看一下控制台,我想其中有很多
this is level 5
。 You have to handle this situation. 您必须处理这种情况。
You can do something like this: 您可以执行以下操作:
void Update () {
if(Input.GetKeyDown(KeyCode.Return) && (CurrentLevel == 0)){
CurrentLevel = 1;
Application.LoadLevel (CurrentLevel);
}
if (CharacterMovement.Score == 10) {
CurrentLevel = 2;
CharacterMovement.Score = 11;
Application.LoadLevel (CurrentLevel);
}
if ((CurrentLevel != 5) && (CharacterMovement.Lives == 0)) { // Change this
isLost = true;
Debug.Log ("is now true");
CurrentLevel = 5;
Debug.Log ("current level is set to 5");
Application.LoadLevel (CurrentLevel);
}
if (CurrentLevel == 5) {
Debug.Log ("this is level 5");
if (Input.GetKeyDown(KeyCode.Tab)) {
Debug.Log ("tab is pressed");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.