SCORE TRACKING ACROSS LEVELS

Hi! I’ll try keep this brief:

I need my game to continue to count the score across levels, so like if it leaves level 1 with 10 points, I need it to start level 2 with 10 points.
I’m trying to use player prefs for this, but it doesnt save between scenes! Can anyone help me out? :slight_smile:

6775004--783491--upload_2021-1-28_12-15-5.png

Just create something to persist between scenes with DontDestroyOnLoad called on it, et voila!

As for actually saving it, you need to look into serialisation and deserialisation as that is what you want to do, serialize this info to some file on their disk :slight_smile:

Good luck!

2 Likes

PlayerPrefs should also persist between scenes, maybe you are accidenlty overriding it.

1 Like

I don’t see you telling PlayerPrefs to save its changes anywhere in that code. As a general rule of thumb you should call the method after you’re finished making changes even if the Get methods appear to be working correctly.

https://docs.unity3d.com/ScriptReference/PlayerPrefs.Save.html

If you just need a GameManager to store stuff for thee mult-scene duration of your game, use a singleton.

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

Unity3D Singleton with Prefab used for predefined data:

These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

public void DestroyThyself()
{
   Destroy(gameObject);
   Instance = null;    // because destroy doesn't happen until end of frame
}