- Home /
How to load player last scene visited
Hello guys! please i need help to understand how to save and load the last scene that player has played, It can be even playerpref anything will be fine! If that is important the game is set up like that, for example we have 3 zones A, B, C and so on. A connected to B, B is connected with A and C, and C with B. ( There are no "completed level" ) What i need to do and have? I can not find a guide for that specific thing... ... Please! Thanks in advice!
Well you can't save an entire scene exactly the way it is with one line of code, you'll actually need to save all of the data and variables basically, store them, and make your own method of recreating the scene in the exact state it was saved in. It's not all that hard really, but if you explain how your game works perhaps we can offer some suggestions
Thanks! I forgot to say that is in 2D, pretty close to the unity Explorer 2D game kit! ( But without the tilemap, the whole scene is drawed in one png with some sprites in addition), i have already set up dont destroy on load on character prefab, everything works fine, i can go on and back through scenes, but for some reason i dont know why im able to Save only in one scene... Even using other saves utility... ... If you need to know more just tell me
Answer by Tomer-Barkan · Nov 16, 2018 at 01:47 PM
Whenever a scene is loaded, save it in player prefs. Then have a method that will load that scene.
private void Awake()
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
PlayerPrefs.SetString("_last_scene_", scene.name);
}
public static void LoadLastScene()
{
SceneManager.LoadScene(PlayerPrefs.GetString("_last_scene_"));
}
Thank you, i guess that's what i wanted! I will try that once i come back home! Any other advice on how should i use It?
Hmm, attach this script to an object that is in your first scene, and add a check in Awake to make sure that it doesn't get executed more than once (use a static bool, and set it to true after registering to sceneLoaded).
Then simply call the static LoadLastScene whenever you want to load the last scene.
sorry for the late reply, i've got fever and i doesn't feel quite well! i'm feeling a bit alright just today... ... I've achieved what i was trying to do! Thanks <3! I have some more question now, should i ask here or i have to open a new post? ( I would like to know how to merge this with enemies death, dialogue and things like that ) thanks again!
Your answer
Follow this Question
Related Questions
Scene Saving 1 Answer
Crash when save scene or load scene. 0 Answers
Missing NavAgent after loading scene? 0 Answers
problem changing scene when using playerPrefs. help. 1 Answer