Published on

Using Unity's PlayerPrefs to save data easily. Easy even for beginners. Can be used by copy and paste [Introduction to Unity]

  • Evelyn
    Full-time IndieDev.
    I'm Japanese, so please forgive me if my English is strange.

Please share this article if you like!

Hello!! Welcome to Evelyn GameDev Blog.

When you create a game, you will always need to save the data.

We would really like to implement the ability to save game scores and stage achievement information.

However, it may be a hurdle for beginners, as they may not know what to do.

Don't worry.

Unity provides a class called PlayerPrefs to make it easy for anyone to implement the save function.

I will explain it in a way that even beginners can easily understand, so please read to the end.

First, let's learn about basic usage, and then let's look at some common questions about storage and safety.

Table of Contents

What are PlayerPrefs?

PlayerPrefs is a class that stores player settings between game sessions. String, floating point, and integer values can be stored in the user's platform registry.

Unity stores the PlayerPrefs data differently depending on which operating system your application runs on. In the file paths listed on this page, the company name and product name are the names you set in Unity's player settings.

How to use PlayerPrefs

Let's see how to use PlayerPrefs in a situation like "saving a score".

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScoreManager : MonoBehaviour
    public int Score = 0; // Score variable
    public string ScoreKey = "SCORE";

    // Initialization process
    void Start ()
        // Loading Scores

    public void SaveScore(){
        // Save your score
        PlayerPrefs.SetInt(ScoreKey, Score);

    public void GetScore()
      // Get your score
        Score = PlayerPrefs.GetInt(ScoreKey, 0);
        Debug.Log("Score:" + PScore);

You can easily save and retrieve data by simply calling the PlayerPrefs method.

About saving

// Save your score
PlayerPrefs.SetInt ("SCORE", 1);
PlayerPrefs.Save ();

SetInt function is used to save the score.

SetInt function to save the score. The first argument is the name of the data to be saved (this will be specified when loading), and the second argument is the number to be saved.

One thing to note is that once you have finished setting up the data to be saved in PlayerPrefs, you need to execute the PlayerPrefs.

Save function, because when the PlayerPrefs.Save function is called, the values registered in PlayerPrefs are actually saved.

Save function is called, the values registered in PlayerPrefs are actually saved. Even if you don't execute the Save method, the data will be saved at the end of the game, but if the game crashes, the data will not be saved!

About loading

// Loading Scores
Score = PlayerPrefs.GetInt ("SCORE", 0);

GetInt function is a function to get "the value stored with the name specified in the first argument" as the return value.

The second argument is a setting of what to return if the value is not saved.

If you understand saving, loading is easy!

Storage of non-Int type

So far, we have seen how to save and load using the SetInt and GetInt functions.

However, as the name suggests, the SetInt and GetInt functions save and load with the Int type, so they don't work well with other types.

So let's see how to save with other types.

Actually, PlayerPrefs provides functions to save in Int, Float, and String format, so saving in other types is easy!

Function names Description
SetInt Sets the value (Int type) of the configuration information identified by key.
GetInt If the key exists, get the value (Int type) corresponding to the key.
SetFloat Sets the value (Float type) of the configuration information identified by the key.
GetFloat If the key exists, get the value (Float type) corresponding to the key.
SetString Sets the value (String type) of the configuration information identified by key.
GetString If the key exists, get the value corresponding to the key (String type).

For example, if you want to save it as a string type, it would look like this!

// Save user name
PlayerPrefs.SetString("USERNAME", "Evelyn GameDev");

Determine if data is stored for a key

There is also a way to check if the saved data exists.

HasKey function.

HasKey function. This function returns treu if the data specified in the argument exists, and false if not.

The main usage of this function is to use an if statement to separate the processing for "if it exists" and "if it does not exist", as shown below.

if (PlayerPrefs.HasKey ("USERNAME")) {
    // Exist
} else{
    // Doesn't exist

About deleting

Deleting is also easy.

PlayerPrefs provides the following two deletion functions.

// Delete all of the stored data
// delete the key specified in the argument
PlayerPrefs.DeleteKey("key to delete");

For example, if you want to delete only the data saved as USERNAME earlier, you can do the following.


Things to keep in mind when using PlayerPrefs

So far we have learned how to use PlayerPrefs.

However, many of you may still have some questions.

Here is a list of frequently asked questions.

Please refer to them.

Where is it stored?

The storage location depends on the environment.


  • ~/Library/Preferences/com.ExampleCompanyName.ExampleProductName.plist


  • HKCU\Software\ExampleCompanyName\ExampleProductName


  • ~/.config/unity3d/ExampleCompanyName/ExampleProductName


  • /Library/Preferences/[bundle identifier].plist


  • /data/data/pkg-name/shared_prefs/pkg-name.v2.playerprefs.xml


  • IndexedDB

Will it be tampered with?

PlayerPrefs are stored in the local environment (in the user's PC or phone).

In other words, the data is in the hands of the user.

It is important to understand that there is a high possibility of analysis and tampering.

In general commercial applications, important data is kept on the server to prevent falsification as a countermeasure.

On the other hand, if your work is a personal work that can tolerate tampering, you can use PlayerPrefs without any problem.

Summary of this article

In this article, we have looked at saving and loading data in Unity.

I hope you have found it quite easy to do so using PlayerPrefs.

As for the last point about analysis and tampering, it would be a shame to delay the progress of the project due to excessive fear.

I hope you can understand how easy it is to use PlayerPrefs.

Use PlayerPrefs to save your data!

Thank you for reading to the end.

I hope it will be useful for your game development.

Please share this article if you like!