Hello I have run into a small problem with my level loading.
The game is a small 2D game where when you hit a trigger you lose the game and it opens a new scene where it says “try again”. When you press “Try Again” I want to be able to load the last played level. I tried using player prefs so I did this:
“PlayerPrefs.SetString = Application.loadedLevelName;”
And then tried to call it in a different script with this line:
“Application.LoadLevel (PlayerPrefs.GetString);”
but unity didn’t like it and i got an error message.
I just need a way to load the last played level when you press a button.
Any and all help is much appriciated.
Well, to store a string in PlayerPrefs you have to provide a “name” for the value you want to save. That name you have to use when you want to read the value back:
// storing the current level
PlayerPrefs.SetString("lastLevel", Application.loadedLevelName);
// loading the stored level or if none was stored yet, the "defaultLevelName".
Application.LoadLevel(PlayerPrefs.GetString("lastLevel", "defaultLevelName"));
SetString() :
http://docs.unity3d.com/ScriptReference/PlayerPrefs.SetString.html
GetString() :
http://docs.unity3d.com/ScriptReference/PlayerPrefs.GetString.html
PlayerPrefs.SetString(Application.loadedLevelName, Application.loadedLevelName);
Application.LoadLevel(PlayerPrefs.GetString(Application.loadedLevelName));
A much easier way is using : http://docs.unity3d.com/ScriptReference/Application-loadedLevel.html
Application.LoadLevel(Application.loadedLevel);