简体   繁体   English

在场景Unity中未检测到输入。 从该场景开始游戏时有效; 否则不起作用

[英]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.

相关问题 统一。 下载后缓存时无法激活场景实例 - Unity. Can't activate scene instance when it cached after download 如何将我的分数游戏 Object 从我的游戏场景转移到 Unity 中的游戏结束场景 - How can I Transfer my Score Game Object from my game scene to game over scene in Unity Unity中的LoadScene()函数何时更改场景? - When does the LoadScene() function in Unity change the scene? Game Over Scene不起作用(Unity C#) - Game Over Scene doesn't work (Unity C#) 从其他场景加载时,Coroutine无法启动 - Coroutine not starting when loading from a different scene 如果我在学分场景之前进入游戏场景,使面板向上滚动的Unity 2018脚本将无法工作 - Unity 2018 script to make panel scroll up wont work if I go to the game scene before the credits scene 从另一个场景加载场景时加载的内容与在编辑器中首先选择该场景时加载不同(Unity,c#) - Scene loads differently when loaded from another scene than selecting that scene first, in the editor (Unity, c#) 重新加载游戏场景时,对象不会统一加载 - When I reload my game scene the objects do not reload in unity 单击按钮以序列化数据和更改场景时,游戏停止,但是Unity没有错误日志 - Game stops when button is clicked to serialize data and change scene, but no error log from Unity 从最后一个场景开始游戏Unity 3D - Start Game from the Last Scene Unity 3D
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM