Have you ever encountered issues like “my score getting reset,” “settings not carrying over,” or “progress disappearing” when switching scenes in Unity? This article carefully explains three methods for persisting and sharing data across scenes, complete with working code examples, to solve these common “Oh no!” moments faced by the growing number of Unity beginners.
Introduction: Why is data migration necessary?
In Unity, variables are generally reset because new objects are generated for each scene. The following types of data require persistence:
- Game progress (e.g., stage, score)
- Player status (e.g., health, equipment)
- Volume settings and UI state
Methods for Sharing Variables
Here are three methods for sharing variables:
- Using global variables
- Sharing variables using DontDestroyOnLoad()
- Sharing variables using PlayerPrefs
1. How to use static variables
Global variables are variables that can be used in any scene within Unity and are shared throughout the entire game. Using global variables allows you to use the value of a variable set in a previous scene in the next scene as well.
Declaration of global variables
To define a global variable, do the following. The class name can be anything you like.
public static class GlobalVariables
{
public static int score = 0;
}
Passing Scene Variables
Next, we’ll explain how to share variables between scenes using global variables. First, you need to pass the variable’s value between scenes.
To pass the variable, you must set the variable’s value to a global variable, as shown in the following example.
GlobalVariables.score = 100;
The score variable is set to 100.
Using Scene Variables
Finally, we’ll explain how to share variable values between scenes using global variables. The following example demonstrates how to use the `score` variable to display the scene’s score.
public class ScoreManager : MonoBehaviour
{
public Text scoreText;
void Start()
{
scoreText.text = "Score: " + GlobalVariables.score;
}
}
In the example above, the Start method of the ScoreManager class uses the global variable `score` to display the score.
Benefits
- Simple and easy to implement
- Instant access from any screen
Points to Note When Using Global Variables
Since variables can be referenced from any scene, when changing a variable’s value, be sure to check which scenes will be affected before making the change.

It’s simple, but managing global variables is going to get tough as they pile up…
2. Variable Sharing Using DontDestroyOnLoad()
DontDestroyOnLoad() is a function that can be used to keep objects persistent between scenes. Using this method ensures that objects remain present and do not disappear when switching scenes.
using UnityEngine;
public class GameManager : MonoBehaviour
{
public static GameManager instance;
public int score;
void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
}
In the Awake() method, if an instance already exists, destroy the new instance; otherwise, set itself to the instance variable.
Failing to do this will cause objects specified with DontDestroyOnLoad() to be created every time the scene is switched.
Value Inheritance Method
The GameManager class created earlier contains a score variable, which can be used in any scene.
For example, switching scenes as shown below will carry over the score.
using UnityEngine.SceneManagement;
using UnityEngine;
public class SceneSwitcher : MonoBehaviour
{
public void SwitchToNextScene()
{
GameManager.instance.score = 100;
SceneManager.LoadScene("NextScene");
}
}
The score is set to 100, and the scene transitions to the next one.
Benefits
- As it can be run as a MonoBehaviour, coroutines and Inspector operations are possible.
- It functions as the only instance that is not destroyed during scene transitions.
3. Persist data using PlayerPrefs
PlayerPrefs is a data storage feature provided by Unity. It is used as a simple save function.

This method saves variables even after you quit the game!
Using PlayerPrefs allows you to share variables between scenes. Here is an example implementation of variable sharing using PlayerPrefs.
First, save the variable’s value in the previous scene. In the following example, the score value is saved under the key “score”.
int score = 100;
// Save score
PlayerPrefs.SetInt("score", score);
Next, load the saved data in the following scene and assign it to a variable. In the example below, the score value is read from the key “score” and assigned to the score variable.
If it is not saved, it returns 0.
// Load score
int score = PlayerPrefs.GetInt("score", 0);
Benefits
- Data persists even after closing the game
PlayerPrefs saves data to your computer’s registry or your smartphone’s internal storage, so it won’t disappear when you close the game.
For example, your high scores and volume settings will be restored exactly as they were the next time you launch the game.
👉 Perfect for save functions and storing settings. - Extremely easy to implement
You can save and load data with just one line of code, making it easy for beginners to use.
Important Notes
- Limited data types can be saved
Only int (integer), float (decimal), and string (text) can be saved. - Security is weak
PlayerPrefs data is not encrypted.
On PCs, the registry can be edited directly; on smartphones, the app’s internal data can be edited directly, making it easy to change values.
Recommended assets for data storage
PlayerPrefs has limitations on the variables it can save, requires writing code, and is cumbersome to use. It also has security vulnerabilities, making this asset a more convenient alternative.
Summary: How to use them appropriately?
- Want to preserve user settings and save data → PlayerPrefs
- Simple “just a little data sharing” → static variables
- Want consistent game management across scenes → Singleton
This article introduced three specific implementation examples for sharing variables during scene transitions. By effectively utilizing these methods, you can efficiently share data between scenes!