Keep your points, even when loading a new scene in Unity.

Esteban Ibarra
2 min readMay 20, 2021

So one of the last challenges was when loading the boss screen, the player would lose all their points which of course would be pretty frustrating. Fortunately, the answer was simple with a bit of caveats, and that was to make the _score variable static. That way, it would stay persistent throughout the entire game.

Just this by itself doesn’t solve the issue. I quickly discovered when I started a new game, the score would stay from the last game and not reset to 0, even when I tried specifically in many ways to do it. Turns out you need to initialize it in the awake function first:

What’s that NewGame doing there? That was the other half of the equation; if I loaded a new scene, and it wasn’t a new game, then keep all the current scores, but if we started a new game then we initialize the score. So where do we tell the player it’s a new game? The main menu loading screen is a good choice and fortunately, there’s a script that loads the main game when the ‘new game’ button is pressed. We can take advantage of that.

The button access the main menu script. This is actually the perfect and most logical place to tell the player it’s a new game and because the Player itself is a singleton, it’s easy to access:

Now our score will persist to whatever screens are loading and won’t be cleared until we specifically want it to!

--

--