Text not saving to next scene

I have a GameObject with an attached script called GamePlay. Underneath the GameObject I have my Canvas and which contains some a few UI text controls. Those text displays, show a score, player lives and level. Pretty basic.

However, when the player loses all three lives, I want the score and level to show onto the next scene. I can either a) get the next scene to load and it doesn’t save the text, or b) the next scene doesn’t load but the text remains on the screen.

Here’s my code:

    private void Awake()
    {
        int numberOfGameSessions = FindObjectsOfType<GamePlay>().Length;

        if (numberOfGameSessions > 1)
        {
            Destroy(gameObject);
        }
        else
        {
            DontDestroyOnLoad(gameObject);

        }

    }

I have the code below which handles the player losing a life:

    public void ProcessPlayerDeath()
    {
        if (numberOfLives > 1)
        {
            TakeALife();
           
        }
        else
        {
            ResetGameSession();
        }
    }

    private void TakeALife()
    {
        numberOfLives--;
        setTexts();
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    public void ResetGameSession()
    {
        SceneManager.LoadScene("EndCredits");
        Destroy(gameObject);

    }

If the player dies we just reload the current scene. That works fine. The problem occurs when the player loses all of their lives. I go to load the “EndCredits” scene in the ResetGameSession method and it won’t load that scene.

Ugh. Any ideas?

Hi @mpodlesny ,

From what I see in your code, you’re calling Destroy(gameObject) in your ResetGameSession method.

This will actually destroy your GameObject and all the GameObjects underneath in his hierarchy.

If you have your texts there, as you mentioned, those texts will be destroyed as well.

Maybe you should handle destroying such GameObject when the player exits the “EndCredits” scene instead.

Good luck with it!

Ok. I will give that a try.

I changed my ResetGameSession method to:

    public void ResetGameSession()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex+1);

    }

And in the Start method of the new scene this is my code:

    void Start()
    {
        gamePlay = FindObjectOfType<GamePlay>();
        playerScoreText.text = "your score: " + gamePlay.playerScore.ToString();
        Debug.Log("BEFORE: " + FindObjectsOfType<GamePlay>().Length.ToString());

        Destroy(gamePlay);

        Debug.Log("AFTER: " + FindObjectsOfType<GamePlay>().Length.ToString());
    }

I have a couple of debug statements in there and noticed the value is 1 before I destroy it and 1 after I destroy it which means it’s not being destroyed … maybe my code is an Avenger? Thanos? who knows, but I am leaning more towards I have something wrong.

As a side note. The next scene loads properly and I am able to get the score text to show up from scene to scene, now I just need to know how to get rid of the previous scene.

Glad that you have it working!

For getting rid of the previous scene, you can use SceneManager.UnloadSceneAsync.

You can check more information about it here: Unity - Scripting API: SceneManagement.SceneManager.UnloadSceneAsync

Good luck with it!