DontDestroyOnLoad will not be deleted when going to the EndGame Scene

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.

5405508--548883--Annotation 2020-01-24 140355.png
5405508--548886--Annotation 2020-01-24 140429.png

Couldnt you just have a script on it where if that scene is loaded it does destroy? Or if that doesnt work or make sense, have a script on your EndGame scene somewhere that will Destroy that gameobject when it loads? Cause DontDestroyOnLoad does exactly that, doesnt mean it cannot be destroyed (correct me if i am wrong).

Sorry for the late reply. That unfortunately is not working. Here is what I am doing. This is inside my level script which is one of the gameobjects being persisted when the player loses a life.

    void Awake()
    {
        //If there are two level objects or if I am loading a fresh level then do not persist level object
        if (GameObject.FindObjectsOfType<Level>().Length > 1 ||
            FindObjectOfType<SceneLoader>().GetFreshLevel())
        {
            Destroy(this.gameObject);
        }
        //If I lost a life and still have lives and this is not a new level then persist and restart scene
        else if (FindObjectOfType<GameStatus>().NumberOfLives > 0 ||
            !FindObjectOfType<SceneLoader>().GetFreshLevel())
            DontDestroyOnLoad(this.gameObject);
        //Else just destroy the level object anyways. 
        else
            Destroy(this.gameObject);
    }

I tried to add a flag if it’s a fresh level and it will not destroy that game object.

I even tried going after it wholesale

Destroy(GameObject.Find(“DontDestroyOnLoad”))

The Find method returns null.

Nevermind!

I figured it out with

Destroy(GameObject.Find(“Level”));

That found the game object and destroyed it. I was right, I was looking for the wrong object. That is if you Destroy(GameObject.Find(“DontDestroyOnLoad”) that’s not a proper game object, that is a scene object or whathaveyou. i need to find the actual game objects and destroy them.

1 Like