- Home /
How do I save the scene that I last played, and then load it at next run-time?
So I am currently working on a multi-level 2D game that requires you to play trough a multitude of short levels, kind of like that old game Red Ball. The difference being that i don't want to have a level selector, but I want the game to remember the last played level and then load it at runtime. I don't need complex saves with positions or health or objects, all I want to save is the last played scene and then load it. The game is based on basic scripts of movement and scene changing.
I'd like to add the fact that I am a COMPLETE newb at scripting so I may need simple explanations.
Answer by tanoshimi · Jul 18, 2017 at 06:39 AM
That's a perfect scenario for using PlayerPrefs. https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data
Answer by Istros · Jul 18, 2017 at 07:11 AM
Hi!I prepared an example code for you.I tried to explain everything clearly I hope it is.You just need to have codes to call the 2 custom functions.Have a good day!
using UnityEngine;
using System.Collections;
public class SceneTracker: MonoBehaviour
{
public int lastloadedsceneIndex;//When building your game every scene has its own index number.Starts from 0.Default start scene is 0.(I will assume this is your main menu or scene loader so first level we will load will have index 1)
void Awake()
{
DontDestroyOnLoad(transform.gameObject);//This makes this gameobject to not to be destroyed when a new level/scene is loaded.So we can track which scene he/she finished lastly.
}
void Start()
{
lastloadedsceneIndex = PlayerPrefs.GetInt("LastLoadedSceneIndex");//When the game starts we get the last saved value for index.
}
public void UpdateLastLoadedSceneIndex()
{
lastloadedsceneIndex++;//We increase index by one.
PlayerPrefs.SetInt("LastLoadedSceneIndex",lastloadedsceneIndex);//We save the last value for index.SetInt takes two arguments.First one is
unique code for our save.Be sure to use same codes for Get and Set functions.Second parameter is the value.You can directly set it as a number but this is not our goal.
}
public void LoadNextScene()
{
Application.LoadLevel(lastloadedsceneIndex + 1);//This method is a little bit old but still works.In paratheses you can write level name(with using "") or level index(without "").
}
}
hi, thanks for your answer but as I am not good with scripting I have no idea where I should use this script, I'm sorry if this is a stupid question, but can you tell me where I should use it?
Hi!'You need to create a Script and name it SceneTracker.Then copy everything and then paste it to the script.It is done but to use it you need to call it from a script.You can call it in 2 different ways but I will show only the first one.
1.Go to a script(or create) that changes the level when you want.
2.In the parameters type this:"public SceneTracker sT;"//We make a reference to SceneTracker Script.
3.Go to start function then type this:"sT = GetComponent();"//We get the SceneTracker Script in the scene.
4.Create a function that calls the SceneTracker.Example:
public void ChangeScene() { st.LoadNextScene();//We call the LoadNextScene function from this script. }
5.Create a empty gameobject and put these 2 scripts into that gameobject.
Done!
Note:For now we do not update the index number.You should create another method to update the index number(Or just call the UpdateLastLoadedSceneIndex function like above)
Hello Istros are you available for chat? i have a few questions about you're script thank you indeed
Your answer
![](https://koobas.hobune.stream/wayback/20220612130508im_/https://answers.unity.com/themes/thub/images/avi.jpg)