- Home /
Load game with Canvas button
I want to create a save game with only the name of the scene in which the player last stopped. I want the game to be saved whenever the player starts the next phase and when he returns to the game, when he presses on the main menu, he returns to where he left off.
Answer by smkmth · Nov 26, 2017 at 12:46 AM
You can get the name of the active scene you a currently in with SceneManager.GetActiveScene
Remeber to add
using SceneMangement;
in the name space.
it actually gets the scene as a reference- so if you want to get the scenes name - i would say public void SaveGame{
string LastSceneName = SceneManagement.GetActiveScene().name;
}
then if its litearlly just the scene name that you need to to save and nothing else, the easiest way to save data is playerprefs. it sounds like you are doing a small project, and if so, i think player prefs might be fine for your purposes, although for a larger project saving data can be more intricate.
https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
heres the docs for player prefs, but ultimatly what you want to do is in the save method i wrote, add somthing like
PlayerPrefs.SetString("SavedSceneName", lastSceneName);
Then just drag and drop the object this method is on to the button's OnClick panel in the inspector. You can load the game with somthing like
public void LoadGame(){
string LoadLevelName = PlayerPrefs.GetString("SavedSceneName", "No Name");
if (LoadLevelName == "No Name"){
Debug.Log("Somthing wierd has gone on, check the name of the scene you tried to save - check the build order, this shouldnt really happen");
} else {
SceneManager.LoadScene(LoadLevelName, LoadSceneMode.Single);
}
Thank you so much, you've solved my problem.
Your answer
Follow this Question
Related Questions
Saving and loading data from file 0 Answers
Serialization scripting error 1 Answer
Cannot load a saved game 1 Answer
Saving/Load using menu C# 1 Answer
Loading a list into unity 0 Answers