How to use PlayerPrefs in Unity

Saving and loading data are a continuous process in any program. But when you have to terminate the program and reload it, then you need to save the data to disk or web to retrieve it later. To solve this issue, we can use Unity PlayerPrefs class.

In game development, you need to save data like how many levels were completed, player’s current level, player’s health and more. These data will be used when the player starts his next session. In this tutorial, we will see how to save and retrieve data using PlayerPrefs and move a step forward to learn Unity.

Video Tutorial

What is PlayerPrefs?

PlayerPrefs is a class that stores and retrieves data from local storage between game-play sessions. PlayerPrefs can save the following

  • Float
  • Int
  • String

Where does PlayerPrefs save the data?

It differs based on the device and operating system. Here is a table with path showing where it saves the data based on device.

Operating SystemSave Location
Mac Os~/Library/Preferences/com.ExampleCompanyName.ExampleProductName.plist
Windows Store App%userprofile%\AppData\Local\Packages\[ProductPackageId]\LocalState\playerprefs.dat
Windows Phonelocal folder
WebGlLocation depends on IndexedDB API

How to save Data using PlayerPrefs

Unity PlayerPrefs uses a string to save the data. This string is called keyname. You need to specify separate keyname for each variable you need to save. PlayerPrefs.Save() should be called before closing the application to save all PlayerPrefs data.

Save float

PlayerPrefs.SetFloat(KeyName, 2.0f);

Save int

PlayerPrefs.SetInt(KeyName, 2); 

Save String


How to retrieve data using Unity PlayerPrefs

To get the data back, you need only the keyname. Remember the keyname is case-sensitive, so you need to be very careful when typing the keyname.


float test = GetFloat("keyname");


int test = GetInt("keyname");


string test = GetString("keyname");

You can set a default value to get function as the second input. If the keyname doesn’t exist then the function will return the default value. Here is an example.

float test = GetFloat("keyname",1.0f);

Delete Data saved using PlayerPrefs

You can check if a Keyname exists using the HasKey function. Let’s see an example on how to check if a keyname exists and delete it, if it does.

void Keycheck(string Test)
        if (PlayerPrefs.HasKey(Test))
            Debug.Log("The key " + Test + "is deleted");

            Debug.Log("The key " + Test + " does not exist");

Delete all keynames and values

This is generally used to reset all game data. You must use it with caution. It’s better to put a confirmation popup before execution the delete all command.


This deletes all stored Keynames and values.

Where to use Unity PlayerPrefs

PlayerPrefs can be used for many purposes. Here are some major uses to note

Is PlayerPrefs Fast?

No, PlayerPrefs is not as fast as the load and saves in scripts. So, it’s not recommended to use PlayerPrefs for in-game computation. It is a good practice to load all your PlayerPrefs data during scene load.

Get all saved PlayerPrefs

It not possible directly through code to view all the saved PlayerPrefs but you can use the asset store plugin called PlayerPrefs ultimate to load all saved PlayerPrefs. This plugin also lets you save and load data visually. If you are someone who doesn’t like to code then this is the way to go.

PlayerPrefs alternative

PlayerPrefs are not safe to save important data. In that case, its better so save your data in a server. It depends on what type of requirement your game has. If you need a lot of data processing then, its best to go with a database engine.

Unity PlayerPrefs are best to store small amount of data. If you have a lot of data to store then best go with binary. For our next tutorial, we will learn how to destroy game objects in Unity.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.