How to keep data across scenes in Unity3D

facebooktwittergoogle_plusredditpinterestlinkedintumblrmail

This brief article will give you a basic setup for preserving data across scenes / levels in Unity3D. It does not show you how to save that data to disk.

We’ll achieve the data transfer by creating an object with a special script that (when attached to an object) is not destroyed when the scene is changed (and keeps the desired data in variables). This object will be created once, when the first scene loads, and it will appear in every subsequent scene you’ve loaded from that initial scene.

Here’s an example script:

 

 

 

In the above script, the line DontDestroyOnLoad(this); is the bit that you need most. The data/variables you can set as you require. I’ve used a static variable for totalScore, so that I can easily call it without having to reference an instance of the object the script is attached to.

If the above script was named KeepScore.js, then to call the variable when I load the next scene, I would use KeepScore.totalScore.

That’s all you should need to know, but we’ll provide a more fleshed out example below, if you are new to all this.

First you’ll need the scene that has data in it you wish to save. I’ve created a new scene called DataScene1.

Create an empty gameobject by going to GameObject > Create Empty, and rename this object to ScoreObject.

Next create a new JavaScript called KeepScore, and attach it to the ScoreObject by dragging the script onto the object in the Hierarchy panel. Images below for illustration.

datau3d-1

datau3d-2

To edit and display data, we’ll create a basic GUI, in a separate script.

Here’s the Javascript (called ScoreGUI), which you can drag onto the Main Camera object (but not the ScoreObject). It’ll allow you to set an number, which will be saved, and will appear in the next scene, which can be loaded by pressing the ‘Load Next‘ button. The script has comments explaining what the statements do.

 

 

Save this scene. In File > Build Settings, select Add Current, if you hadn’t already done so. Image below for illustration.

datau3d-3

Load a new scene: File > New Scene. Save this new scene with the name DataScene2.

Create a new JavaScript called ShowScore, and drag this onto the Main Camera object. This script is a very basic GUI that will show us the number we set in the previous scene.

 

 

 

Save this scene. In File > Build Settings, select Add Current, if you hadn’t already done so. Image below for illustration.

datau3d-4

Now load the first scene and test. Click to increment the score, and click the Load Next button. The next scene should load displaying whatever score you created. You’ll see in the Hierarchy Panel that the ScoreObject remains between scenes. The first image below shows the first scene, and the second image shows the preserved score displayed in the second scene.

datau3d-5

datau3d-6

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">