Skip to main content

Using PlayerPrefs to save simple data

There is no live demo for this example due to saving and loading states not being available in WebGL.

PlayerPrefs is useful for storing simple pieces of data about a game.

It can store the following data types:

  • int
  • float
  • string

In this example there is a level with a player that can be moved around.

There are two buttons on the scene. One for save and one for load

Save will save the current x,y, and z position of the player.

Load will load the stored x,y, and z position of the player.

First add a canvas with two buttons and name then SaveButton and LoadButton.

Position the buttons appropriately.

When using the new Input System you will have to enable it on the EventSystem object.

Click Replace with InputSystemUIInputModule.

Now we have our UI set up we want to create a script to save and load data. We will attach this to the EventSystem and call the script LocationManager.

Open the script in your editor of choice.

We need to create two different methods. One for saving and one for loading the PlayerPrefs data.

Create a method SavePlayerPosition() this will be used to save the player x,y, and z values to different floats.

First we need to access the player object. We have assumed that the player object has been tagged with the tag Player

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

public class SaveLocation : MonoBehaviour
{

public void SavePlayerPosition() {
GameObject player = GameObject.FindGameObjectWithTag("Player");

}
}

To set values using PlayerPrefs we can use the SetFloat method (there are also SetInt and SetString methods).

This method takes two arguments. The first is the name of the parameter to store in PlayerPrefs which is a string. The second is the value to store in the PlayerPrefs file.

In the example below we are accessing the x,y, and z float values from the player object by using player.transform.position.x and y and z respectively.

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

public class SaveLocation : MonoBehaviour
{

public void SavePlayerPosition() {
GameObject player = GameObject.FindGameObjectWithTag("Player");

PlayerPrefs.SetFloat("player_x_pos", player.transform.position.x);
PlayerPrefs.SetFloat("player_y_pos", player.transform.position.y);
PlayerPrefs.SetFloat("player_z_pos", player.transform.position.z);
}
}

You might want to use a Debug.Log() call to test that this is working.

To read the values from PlayerPrefs we use the Get methods.

There is GetInt, GetFloat and GetString. These take one argument, a string which is the name of the value to read in. This needs to be assigned to a value.

Below on line 17 we find the player object. This will be used later to update the transform position of the player object.

On lines 19-21 we create three float variables x, y, and z. Each of these gets a float from PlayerPrefs through the GetFloat method. For the x value we use PlayerPrefs.GetFloat("player_x_pos"). This is repeated for the y and z values.

We then create a Vector3 to store the new position in 3D space, as shown on line 22.

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

public class SaveLocation : MonoBehaviour
{

public void SavePlayerPosition() {
GameObject player = GameObject.FindGameObjectWithTag("Player");

PlayerPrefs.SetFloat("player_x_pos", player.transform.position.x);
PlayerPrefs.SetFloat("player_y_pos", player.transform.position.y);
PlayerPrefs.SetFloat("player_z_pos", player.transform.position.z);
}

public void LoadPlayerPosition() {
GameObject player = GameObject.FindGameObjectWithTag("Player");

float x = PlayerPrefs.GetFloat("player_x_pos");
float y = PlayerPrefs.GetFloat("player_y_pos");
float z = PlayerPrefs.GetFloat("player_z_pos");
Vector3 loadedPosition = new Vector3(x, y, z);
}

}

Now we need to update the location of the player.

On line 24 we disable the player. This allows us to update the position.

On line 25 we assign the new Vector3 position we have created / loaded to the position of the player object which we access through player.transform.position.

Finally on line 26 we set the player to being active again.

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

public class SaveLocation : MonoBehaviour
{

public void SavePlayerPosition() {
GameObject player = GameObject.FindGameObjectWithTag("Player");

PlayerPrefs.SetFloat("player_x_pos", player.transform.position.x);
PlayerPrefs.SetFloat("player_y_pos", player.transform.position.y);
PlayerPrefs.SetFloat("player_z_pos", player.transform.position.z);
}

public void LoadPlayerPosition() {
GameObject player = GameObject.FindGameObjectWithTag("Player");

float x = PlayerPrefs.GetFloat("player_x_pos");
float y = PlayerPrefs.GetFloat("player_y_pos");
float z = PlayerPrefs.GetFloat("player_z_pos");
Vector3 loadedPosition = new Vector3(x, y, z);

player.SetActive(false); // Disable the player object before moving
player.transform.position = loadedPosition; // Update player position
player.SetActive(true); // Enable player position
}

}

Test the program.

Move the player and press save.

Move the player to a different location.

Press Load

The player should have moved to the saved location.

Note that as we have only saved the x, y, and z position the rotation and the camera look are not saved.

Complete Code

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

public class SaveLocation : MonoBehaviour
{

public void SavePlayerPosition() {
GameObject player = GameObject.FindGameObjectWithTag("Player");

PlayerPrefs.SetFloat("player_x_pos", player.transform.position.x);
PlayerPrefs.SetFloat("player_y_pos", player.transform.position.y);
PlayerPrefs.SetFloat("player_z_pos", player.transform.position.z);
}

public void LoadPlayerPosition() {
GameObject player = GameObject.FindGameObjectWithTag("Player");

float x = PlayerPrefs.GetFloat("player_x_pos");
float y = PlayerPrefs.GetFloat("player_y_pos");
float z = PlayerPrefs.GetFloat("player_z_pos");
Vector3 loadedPosition = new Vector3(x, y, z);

player.SetActive(false); // Disable the player object before moving
player.transform.position = loadedPosition; // Update player position
player.SetActive(true); // Enable player position
}

}