loading...
Cover image for Managing HighScore with Unity's PlayerPrefs

Managing HighScore with Unity's PlayerPrefs

gamedev4k profile image Game Dev 4K ・1 min read

In the last couple of days, I added a way to save the high-score in my game using Unity's PlayerPrefs API.

Also, I improved the end screen of the game.

PlayerPrefs Usage

Using the PlayerPrefs API is very simple. Here's my high-score saving logic looks like:

private bool SaveHighScore(int newScore)
{
    int highScore = PlayerPrefs.GetInt("HighScore", 0);
    bool gotNewHighScore = newScore > highScore;

    if (gotNewHighScore)
    {
        PlayerPrefs.SetInt("HighScore", newScore);
        PlayerPrefs.Save();
    }

    return gotNewHighScore;
}

Discussion

markdown guide
 

I've seen PlayerPrefs being used to store general game data before, but I have a hard time with this because I feel like the intention of this variable is not being applied. The name would suggest it should only be used to store preference data (volume, settings, controls....)

I may be in the minority here though. @eranelbaz made a video that applies this same principal, and I've found a ton of other examples online that take your similar approach :)

What are your thoughts? Technically speaking High Score is not a player preference :)

 

In pure English terms, yes it is :D
I was simply looking for some storage where we can use anywhere. It seems like this will get the job done.

Do you have any other solutions? I am keen to know.

 

I don't, but I think perhaps the community's push to use this feature in a more generic sense should move unity to re-think how they promote this feature in the future. I really like what you did here! I would not be surprised if unity decides to deprecate this property in favor of a more generic one in a later version!

Oh yes. I'm up to a proper API for cases like this for sure.

 

it would be better to encrypt such values because playerprefs are stored as it is to fs. it's pretty easy to set up assetstore.unity.com/packages/tool... .

 

Oh yes. That's a good idea. So, others cannot modify these manually/easily :D

 

What platforms are you planning on releasing the game to?

Also what services are you looking into to do player tracking to find bugs?

 

This is made for iOS but planning to publish on Mac/Windows too.
I want to get the full game publishing experience with this.
Not necessary as a successful game :D

I tried using Playfab & GameAnalytics. But I don't think they don't provide what I need. (Or hard to use)
So, planning to use Mixpanel & Sentry. I used them extensively, so do the job done well.

 

I believe you can easily setup crashlytics, analytics provided by Unity. You don't need to have the extra stuff be imported, open services tab and add whatever you need. then you can add integrations for crashlytics in their dashboard to autocreate the issues in trello or post message to slack. you can see the stacktrace and it's free to use.

Oh! That's interesting. I didn't know about that.