I run the following code each time my scene loads:
Code: Select all
void Start()
{
if (SaveInformation.name == null)
{
SceneManager.LoadScene("Main menu");
}
if (ES3.KeyExists("Dynamics", "saves/"+SaveInformation.name+"/"+SceneManager.GetActiveScene().name+".s"))
{
foreach (GameObject child in GameObject.FindGameObjectsWithTag("Dynamic") )
{
Destroy(child);
}
allDynamicItems = ES3.Load<GameObject[]>("Dynamics", "saves/" + SaveInformation.name + "/" + SceneManager.GetActiveScene().name + ".s");
foreach (GameObject item in allDynamicItems)
{
Instantiate(item);
}
}
}
void OnApplicationQuit()
{
ES3.Save<string>("scene", SceneManager.GetActiveScene().name, "saves/" + SaveInformation.name + ".s");
ES3.DeleteDirectory("saves/" + SaveInformation.name + "/" + SceneManager.GetActiveScene().name + ".s");
ES3.Save<GameObject[]>("Dynamics", GameObject.FindGameObjectsWithTag("Dynamic"), "saves/" + SaveInformation.name + "/" + SceneManager.GetActiveScene().name + ".s");
}
If a scene have saved items and I load it the first time the items only get instantiated in the *Instantiate(item)* row. The second time I load the scene the items also get instantiated on the *allDynamicsItem = ES3.Load<GameObject[]>* row + on the Instantiate(item) row = I get double items in the scene ( and next time 4,8,16 and so on)
Do you have any idea why it works flawlessly the first time I load but not the second time?
//Alexander