I have a script where you click a button and it loads the saved game
I use a singleton class called GameControl to keep track of data between scenes and for saving.
i.e. if you save the game in scene A, GameControl.control.active_scene will equal “A”
so one save game might have active scene as A and another as B
before hitting load the active scene is A, if I load a game the active scene is still A because I overwrite gamecontrol with the original scene, however if I load a different game active scene is now whatever was in the save game.
public void Load(string name)
{
string scenename;
scenename = GameControl.control.active_scene;//save active scene name
Debug.Log("active scene 1+ " + GameControl.control.active_scene);
foreach (Game a in instance.savedGames)
{
if (a.name == name)
{
Debug.Log("loading" + name);
GameControl.control = a.ctrl;
GameControl.control.active_scene = scenename; //set active scene name. because you might have saved in a different scene
Debug.Log("active scene 2+ " + GameControl.control.active_scene);
break;
}
}
I’m a little confused by your question. What exactly do you wan to have happen, and what exactly is happening. This:
makes no sense whatever.
Are you saying your in Scene A… and loading a game that was saved in Scene B… and you want to load that game, but stay in Scene A? or do you want the game to move on to Scene B?
It looks like your code is doing the first thing. Saving the fact that we are in Scene A. Then loading a game that was saved with Scene B… and resetting the Scene to Scene A
Perhaps print out what the Debug Logs are showing in the situation you consider and error and what you expect them to print?
Correct. What I expect to happen is no matter what game I load, I expect scenename to always be what was originally in
GameControl.control.active_scene
however from the debug logs it changes to a different value.
say originally GameControl.control.active_scene =“high score”
after this line
scenename = GameControl.control.active_scene;
scenename is now “high score”
when the game is loaded in the for loop, GameControl.control.active_scene is now “load screen”
however after this line
GameControl.control.active_scene = scenename
GameControl.control.active_scene should be “high score” but it is “load screen”
Well something else is going on. Because your code there should definitely not be doing that:
Try this code and see what the Debug says:
if (a.name == name)
{
Debug.Log("loading" + name);
GameControl.control = a.ctrl;
Debug.Log("After load: GameControl.control.active_scene = " + GameControl.control.active_scene);
Debug.Log("My saved Scene = " + scenename);
GameControl.control.active_scene = scenename; //set active scene name. because you might have saved in a different scene
Debug.Log("active scene 2+ " + GameControl.control.active_scene);
break;
}
When I start the game and hit load the first time I get:
loadingaaa
After load: GameControl.control.active_scene = load screne
My saved Scene = high score
active scene 2+ high score
when I hit load again I get:
loadingaaa
After load: GameControl.control.active_scene = high score
My saved Scene = high score
active scene 2+ high score
Well it would be good to see the Debug right before loadingaaa the active scene 1+ line
However, something odd is clearly going on with scenename:
After load: GameControl.control.active_scene = load screne
My saved Scene = high score <----- Its changed to high score! your earlier code set it to load screne
active scene 2+ high score
after more debugging I found the change takes place after reading the file. but this is truly bizarre as I have not yet added the contents of the data to Gamecontrol at this point!!!
the before and after should be exactly the same!
public void ReadToFile()
{
Debug.Log("before read to file: GameControl.control.active_scene = " + GameControl.control.active_scene);
Strdata = File.ReadAllText(Application.persistentDataPath + "/save.sav");
string decText = AvoEx.AesEncryptor.DecryptString(Strdata);
data = fsJsonParser.Parse(decText);
fsSerializer _serializer = new fsSerializer();
_serializer.TryDeserialize(data, ref instance.savedGames).AssertSuccessWithoutWarnings(); //FSdata data, Type, object result*/
Debug.Log("after read to file: GameControl.control.active_scene = " + GameControl.control.active_scene);
}