my game has 5 scenes, the last scene being the end game scene. If the player loses a life, I use DontDestroyOnLoad in order to preserve the level’s state and restart the scene. The problem is when I load the EndGame scene, the DontDestroyOnLoad persists, and I need it to go away. Here is currently what I have as code.
This is contained in my level.cs script. This contains the level sprites that I am persisting if the player loses a life. Notice that I try not to call DontDestroyOnLoad when I run out of lives. This was one idea but it didn’t work.
void Awake()
{
if (GameObject.FindObjectsOfType<Level>().Length > 1)
Destroy(this.gameObject);
if(FindObjectOfType<GameStatus>().NumberOfLives > 0)
DontDestroyOnLoad(this.gameObject);
}
I have this same exact code in my other GameObject that is being persisted, GameStatus.
These are the two GameObjects I am persisting when the player loses a life. Whenever I move to another scene, a DontDestroyOnLoad gameobject persists with the Level object.
Here is my LoadNextScene code and GameOver
public void LoadNextScene()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex + 1);
}
public void GameOver()
{
int currentSceneIndex = SceneManager.sceneCountInBuildSettings - 1;
SceneManager.LoadScene(currentSceneIndex);
}
Perhaps I am using the singleton wrong, or perhaps there is a way to stop using Persistence on a gameobject and reload that game object.
Here is a screen shot of level 3 when the level 3 object is loaded and a screen shot of the end game scene when loaded and you can see that the DontDestroyOnLoad is still there. Because of that, the sprites from that level show up on my endgame screen.
Either way I’m stuck and could use a hint.
Thank you.