You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity答题游戏生命值归零时无法加载GameOver场景求助

Hey Natasa, let's fix those issues one by one. I see two main problems here: the GameOver scene isn't loading properly when lives hit 0, and your restart button isn't working. Let's break down the fixes:

1. Fixing the GameOver Scene Loading

First, let's look at your Update method. Right now you're using LoadSceneMode.Additive which adds the GameOver scene on top of your current game scene instead of replacing it. Plus, since Update runs every frame, it's trying to load the scene over and over, which can cause weird behavior. Also, when lives hit 0, your TransitionToNextQuestion coroutine is still running and reloading the current scene, which keeps the game going.

Here's how to fix it:

  • Add a flag to prevent multiple scene loads
  • Use LoadSceneMode.Single to replace the current scene
  • Stop all running coroutines when lives hit 0 to halt the game flow

Modify your GameManager code like this:

public class GameManager : MonoBehaviour {
    // Add this flag to track if we're already loading the GameOver scene
    private bool isGameOver = false;

    // ... rest of your existing variables ...

    private void Update() {
        levelOutput = levelValue / 3;
        Level.text = string.Format("Level: {0}", levelOutput);
        Lives.text = string.Format("Lives: {0}", livesValue);
        Score.text = string.Format("Score: {0}", scoreValue);

        if (livesValue <= 0 && !isGameOver) {
            isGameOver = true;
            // Stop all coroutines to prevent further scene reloads
            StopAllCoroutines();
            // Load GameOver scene and replace the current one
            SceneManager.LoadScene("GameOver", LoadSceneMode.Single);
        }
    }

    // ... rest of your existing methods ...
}

Also, make sure your GameOver scene is added to the Build Settings! Go to File > Build Settings, then drag your GameOver scene from the Project window into the Scenes In Build list. If it's not there, SceneManager can't find it at all.

2. Fixing the Restart Button

The restart button isn't working because you haven't added a script to handle the click. Let's create a simple script for the GameOver scene:

  1. Create a new C# script called GameOverManager
  2. Attach it to a GameObject in your GameOver scene (like an empty manager object)
  3. Add this code:
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameOverManager : MonoBehaviour {
    // Assign your main game scene name in the Inspector (e.g., "MainGame")
    public string mainGameSceneName;

    public void OnRestartButtonClicked() {
        // Reset all static variables from GameManager to start fresh
        GameManager.scoreValue = 0;
        GameManager.livesValue = 2;
        GameManager.levelValue = 3;
        // Clear unanswered questions to reset the question pool
        GameManager.unansweredQuestions = null;

        // Load the main game scene
        SceneManager.LoadScene(mainGameSceneName, LoadSceneMode.Single);
    }
}

Then, in your GameOver scene:

  • Select your restart button
  • In the Inspector, add an On Click () event
  • Drag the GameOverManager GameObject into the event slot
  • Select GameOverManager > OnRestartButtonClicked() from the dropdown menu
3. Extra: Prevent Negative Lives

Right now, if the player keeps answering wrong after lives hit 0, livesValue will go negative. Let's fix that in your answer methods:
In both UserSelectTrue and UserSelectFalse, replace this line:

livesValue = livesValue - 1;

With this:

livesValue = Mathf.Max(livesValue - 1, 0);

This ensures lives never drop below 0, keeping your logic clean.

That should fix both your scene loading and restart button issues! Let me know if you run into any other snags.

内容的提问来源于stack exchange,提问作者Natasa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:14:39