- Home /
Trouble with DestroyOnLoad,Game won't start after player dies once
I am just starting out, so I'm coding checkpoints into my game and in my script I include DestroyOnLoad. Issue is when the player respawns after the first death, when the player dies the second time, the game keeps running and it's doesn't restart.
If this information helps, the game I'm making is from Brackey's YT tutorial how to make a game playlist. I'm using the game to experiment new things as I learn more.
My Game Manager Script
`using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
bool GameEnd = false;
public float restartdelay = 1f;
public GameObject youwinUI;
public static GameManager instance;
public Vector3 lastcheckpoint;
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(instance);
}
else
{
Destroy(this.gameObject);
}
}
public void levelwin()
{
youwinUI.SetActive(true);
}
public void EndGame()
{
if (GameEnd == false)
{
GameEnd = true;
Debug.Log("you lose");
Invoke("Restart", restartdelay);
}
}
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}`
Answer by lcplrice · Dec 16, 2020 at 04:41 AM
You are not setting the GameEnd variable back to false on Restart. So in the second playthrough its already true so line 35 will never execute.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Order of destroy objects and OnDestroy event 2 Answers
Unregister Singleton Events before OnDestroy is called 1 Answer
Using Singleton but getting a null reference 2 Answers
Where would I call the DoDestroyOnLoad function on my Singleton Script 1 Answer