Loading and Saving
Trying to implement a basic saving system into my ball rolling game. Basically the functionality I am trying to achieve is it auto saves while you're playing, and on the menu, there is a Load button. This is my saving class, attached to a game object using UnityEngine; using System.Collections;
public class saving : MonoBehaviour {
public static int currentLevel;
void Awake()
{
DontDestroyOnLoad (transform.gameObject);
currentLevel = PlayerPrefs.GetInt("LastLevelLoaded");
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Application.loadedLevel != currentLevel)
{
currentLevel = Application.loadedLevel;
PlayerPrefs.SetInt("LastLevelLoaded",currentLevel);
}
}
}
And this is a loader, attached to a game object as well using UnityEngine; using System.Collections;
public class Load : MonoBehaviour {
void Awake()
{
DontDestroyOnLoad (transform.gameObject);
}
public void Loaders()
{
Application.LoadLevel (saving.currentLevel);
// Call the LevelManager and set the last level.
}
}
When I initially press load on the menu, it reloads my "loading" scene, and when I go to play the main level, and then I click menu, and attempt to load it, it does nothing. It loads the menu again I believe.
Answer by TheAltbacker · Nov 24, 2015 at 08:36 PM
I think you could just exclude your Menu(s) or scenes you don't want the script to save by adding
(Application.loadedLevel != (index of the menu you want to exclude))
to your if statement in the Update() of saving. This way, if the scene you loaded is included in the if statement, it won't be saved. You could also use
(Application.loadedLevelName != "MENUNAME" ))
to refer to your scenes by their names if that is easier. There might be better, more elegant solutions to this problem, but I believe this should work. Cheers.
W.B.
Thanks! I added that in however It still will not load the level
Are all your references okay? I mean, are you sure, that everything gets called when and how you want it to? You might want to Debug your script.
Yeah, it still wouldn't go through, so I ended up changing the idea and went with a world unlock tutorial: http://www.thegamecontriver.com/2014/09/create-level-lock-unlock-system-unity-46.html
Answer by apie2546 · Nov 27, 2015 at 06:26 PM
I solved this by just using a level unlock system: http://www.thegamecontriver.com/2014/09/create-level-lock-unlock-system-unity-46.html
Your answer
Follow this Question
Related Questions
Saving/loading inherited class scripts (C#) 1 Answer
Save and Load Audio from web 0 Answers
Saving and Loading from different scene 0 Answers