- Home /
Display a GameOver scene then restart the game
Hello, I'm trying to display the gameOver scene I made and after some time, like 5s, restart the game by loading the game scene.
I made this, but it doesn't restart the game and stays on the gameOver scene.
using UnityEngine;
using UnityEngine.SceneManagement; // Quand on à besoin de faire appel à une autre scene
public class GameManager : MonoBehaviour {
bool gameEnd = false;
public float restartDelay = 3f;
public void EndGame()
{
if (gameEnd == false)
{
gameEnd = true;
Debug.Log("Game OVER");
Invoke("Restart", restartDelay); // Invoke sert à appeller la fonction à partir d'un certain temps
SceneManager.LoadScene("Terrain", LoadSceneMode.Additive);
SceneManager.LoadScene("FinLoose");
}
}
void Restart ()
{
SceneManager.SetActiveScene (SceneManager.GetSceneByName("Terrain"));
}
}
And what problem are you facing with your current code?
What I see it that Restart
will never be called because you load the FinLoose
scene. Thus, the $$anonymous$$onoBehaviour is destroyed, preventing Restart
from being called.
I need to change to $$anonymous$$onoBehaviour then ? I want to load the scene "Terrain" after 3 or 5 seconds, but still show the GameOver scene.
Answer by Martin_Gonzalez · Mar 30, 2018 at 10:17 PM
your are calling SceneManager.LoadScene("FinLoose");
so i guess this object die when changing the scene.
Just try not calling SceneManager.LoadScene("FinLoose");
and see what happens. If the restart scene appears then i what i told you.
If it is what i told you you need to add the restart code in the FinLoose scene
When I do not call this scene, the games restarts just like before, but I don't get to see the scene with the score.
This is the answer.
You are calling Scene$$anonymous$$anager.LoadScene("FinLoose");
so when Unity load that scene, every object in the scdne will be Destroy (unless DontDestroyOnLoad was called in any object component).