Hello everyone,
I am attempting to make a save and load feature for my game in unity 5.5.
It works reasonably well already, I can save and load my data and when I am in the required scene everything is loaded correctly.
But when I load from my main menu I prompt a scene changes after which I initialize my game objects, but when the game is done loading everything is empty.
I figure this happens because the game objects are made before the scene finishes loading and thus when the scene does change it goes into a empty scene because everything was loaded into the old one. Now what I don’t know how ever is how to fix this problem.
This is my current code:
public void LoadGame(int loadNumber){
SceneManager.LoadScene("Scenes/Prologue");
if (File.Exists(Application.persistentDataPath + "/playerInfo" + loadNumber + ".dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo" + loadNumber + ".dat", FileMode.Open);
SaveGameData data = (SaveGameData)bf.Deserialize(file);
file.Close();
InitaliseGameComponents();
var eventString = _GameProgress.Load(data.GameProgressData);
var splitEventString = eventString.Split(':');
if (data.DialogData != null)
{
_DialogControl.Load(data.DialogData, splitEventString[1], splitEventString[2]);
_DialogDisplay.SetActive(true);
}
if (data.CombatData != null)
_CombatControl.Load(data.CombatData);
}
}
Even though the SceneManager acts first the gameobjects that are being created in the _CombatControl are unloaded. Does anyone know a good fix for this? Or am I doing something fundamentally wrong?
Thank you in advance.