Re: Persist Scriptable Objects between scenes
Posted: Fri Sep 25, 2020 6:46 am
Hello,
These objects are not created during runtime, these are part of the scene.
I believe that aaaaaaaall my problems are related to something that I am missing about updating references or whatever, because some things that were not working, now are magically working. Should I press some button or run whatever command?
I have a clear example about this issue. I was testing the saving of the "collectables", that were not saving properly. After many tries, now this is working, so I decided to replicate the same code with the "triggers". This is the related code:
As you can see, whit the save method I create this new key with the different elements. The code is exactly the same for "collectables" than "triggers", but collectables are working and triggers not.
The same was happening before with the inventory, that was not working and now is magically working.
I am sure that I am missing something that will solve all these issues.
Thank you in advance.
These objects are not created during runtime, these are part of the scene.
I believe that aaaaaaaall my problems are related to something that I am missing about updating references or whatever, because some things that were not working, now are magically working. Should I press some button or run whatever command?
I have a clear example about this issue. I was testing the saving of the "collectables", that were not saving properly. After many tries, now this is working, so I decided to replicate the same code with the "triggers". This is the related code:
Code: Select all
public class SaveManager : MonoBehaviour
{
[SerializeField] private List<Collectable> collectablesToSave;
[SerializeField] private List<Trigger> triggersToSave;
[SerializeField] private List<Room> roomsToSave;
private static SaveManager sharedInstance;
private void Awake()
{
sharedInstance = this;
UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded;
}
public static SaveManager GetSharedInstance()
{
return sharedInstance;
}
public void SaveGame(Player player, int sceneIndex)
{
ES3.Save("player", player);
ES3.Save("inventory", player.Inventory);
ES3.Save("scene_index", sceneIndex);
ES3.Save("collectables_" + SceneService.GetCurrentSceneIndex(), collectablesToSave);
ES3.Save("triggers_" + SceneService.GetCurrentSceneIndex(), triggersToSave);
ES3.Save("rooms_" + SceneService.GetCurrentSceneIndex(), roomsToSave);
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
var player = SearchService.SearchGameObjectByTag(TagName.PLAYER);
if (ES3.KeyExists("player") && player != null)
{
ES3.LoadInto("player", player.GetComponent<Player>());
player.transform.position = player.GetComponent<Player>().CheckPoint;
}
if (ES3.KeyExists("inventory") && player != null && player.GetComponentInChildren<Inventory>() != null)
{
ES3.LoadInto("inventory", player.GetComponentInChildren<Inventory>());
}
if (ES3.KeyExists("collectables_" + scene.buildIndex))
{
ES3.LoadInto("collectables_" + scene.buildIndex, collectablesToSave);
}
if (ES3.KeyExists("triggers_" + scene.buildIndex))
{
ES3.LoadInto("triggers_" + scene.buildIndex, triggersToSave);
}
if (ES3.KeyExists("rooms_" + scene.buildIndex))
{
ES3.LoadInto("rooms_" + scene.buildIndex, roomsToSave);
}
}
}
The same was happening before with the inventory, that was not working and now is magically working.
I am sure that I am missing something that will solve all these issues.
Thank you in advance.