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:
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.
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.