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:
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.Singleto 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.
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:
- Create a new C# script called
GameOverManager - Attach it to a GameObject in your GameOver scene (like an empty manager object)
- 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
GameOverManagerGameObject into the event slot - Select
GameOverManager > OnRestartButtonClicked()from the dropdown menu
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

