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.
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!
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)
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.
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 "").