- Home /
how to save the progress on scene1 when it goes to scene2, so when it goes back to scene1 its still the same saved scene
hello, I have a 2D game that I created that when I pause on the level1.scene it has buttons that will take him to the shop menu which is in the mainmenu.scene and so if the player is done with its purchases or wants to go back to the game(level1.scene), how do I make it so that the level1.scene is still on pause on the same spot where he left.
There are several answered Qs on this. Try a search "unity go back to scene", "unity restore scene" ... . It's not easy, and some of the suggestions are ways make it look like a scene change, when it really isn't.
Answer by Eudaimonium · Jul 18, 2015 at 06:09 PM
I wouldn't suggest using PlayerPrefs for that. It's saving things in Registry, basically.
You need an object that persists during the entire session (cross-scene object that doesn't get destroyed). You save your stuff into that object, transition the scene, and then load from it.
As per one of the Unity tutorials, here is a basic structure of my cross-scene object, called Global Control, which uses a Singleton design concept.
public class GlobalControl : MonoBehaviour
{
public static GlobalControl Instance;
//here put all variables you're going to need
//Pseudo-singleton concept from Unity dev tutorial video:
void Awake ()
{
if (Instance == null)
{
DontDestroyOnLoad(gameObject);
Instance = this;
}
else if (Instance != this)
{
Destroy (gameObject);
}
}
//here put all aditional functions you need across entire game, example:
public void SaveToMemory()
{
//Tell all subscribed objects to save themselves in the scene data - this is my example using .NET delegate and event system.
if (OnSave != null)
OnSave(null, null);
}
}
//Later anywhere in code, call:
GlobalControl.Instance.myVariableThatNeedsSaving = importantStuff;
//or:
GlobalControl.Instance.SaveToMemory();