Lets Build Community

Lets Build is a community of 163 amazing members

We're a place where indie game developers to connect with one another to share ideas, ask for help and teach each other.

Create new account Log in
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

pic
Editor guide
Collapse
rcarlson profile image
Robert Carlson

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

Collapse
gamedev4k profile image
Game Dev 4K Author

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.

Collapse
rcarlson profile image
Robert Carlson

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!

Thread Thread
gamedev4k profile image
Game Dev 4K Author

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

Collapse
oivoodoo profile image
Alexandr Korsak

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

Collapse
gamedev4k profile image
Game Dev 4K Author

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

Collapse
cpluta profile image
Chris Pluta

What platforms are you planning on releasing the game to?

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

Collapse
gamedev4k profile image
Game Dev 4K Author

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.

Collapse
oivoodoo profile image
Alexandr Korsak

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.

Thread Thread
gamedev4k profile image
Game Dev 4K Author

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