I'm having this issue where PixelCrushers.SaveSystem.sceneLoaded is called multiple times when I only should be loading the scene once. See below on how i'm subscribing and then StartMicroGames is called by an Event I created. I can see that StartMicroGames is called once, but then i see multiple OnSceneLoaded calls.
StartMicroGames is called by a button using UnityEngine.UIElements Button.
I logged out StartMicroGames and it was only getting called once and logging out OnSceneLoaded showed multiple times. Below you can see that `Game State Changed from MICROGAMES to MICROGAMES` is called multiple times.
Button 'Games' clicked.
Starting microgame from TabletUIController
Starting MicroGames Session
Game State Changed from PREGAME to MICROGAMES
Game State Changed from MICROGAMES to MICROGAMES
Game State Changed from MICROGAMES to MICROGAMES
Game State Changed from MICROGAMES to MICROGAMES
Starting Random Game
I was also getting some errors like my gameobject was getting destroyed and put back. Which I wasn't expecting because i'm loading additively.
MissingReferenceException: The object of type 'CountDownTimer' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.MonoBehaviour.Invoke (System.String methodName, System.Single time) (at <>:0)
CountDownTimer.Begin () (at Assets/TrickOrTreatSim/Scripts/Controllers/CountDownTimer.cs:24)
TrickOrTreatSim.MicroGamesSceneManager.OnSceneLoaded (System.String sceneName, System.Int32 sceneIndex) (at Assets/TrickOrTreatSim/Scripts/GameManagement/MicroGamesSceneManager.cs:50)
PixelCrushers.SaveSystem.FinishedLoadingScene (System.String sceneName, System.Int32 sceneIndex) (at Assets/Plugins/Pixel Crushers/Common/Scripts/Save System/SaveSystem.cs:997)
PixelCrushers.SaveSystem.OnSceneLoaded (UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode mode) (at Assets/Plugins/Pixel Crushers/Common/Scripts/Save System/SaveSystem.cs:376)
UnityEngine.SceneManagement.SceneManager.Internal_SceneLoaded (UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode mode) (at <>:0)
I decided to go back to using the sceneManager for now and i haven't seen the issue come up
public void StartMicroGames()
{
Debug.Log("Starting MicroGames Session");
var ao = SceneManager.LoadSceneAsync(MiniGameSceneName, LoadSceneMode.Additive); // Load the microgames scene additively
ao.completed += (asyncOperation) =>
{
Debug.Log("MicroGames Scene Loaded");
OnSceneLoaded(); // Call the method to handle the loaded scene
};
}
using UnityEngine;
using PixelCrushers;
public class ReportSaveSystem : MonoBehaviour
{
private void Start()
{
SaveSystem.sceneLoaded -= OnSceneLoaded;
SaveSystem.sceneLoaded += OnSceneLoaded;
}
private void OnSceneLoaded(string sceneName, int sceneIndex)
{
Debug.Log($"Loaded Scene {sceneName}:{sceneIndex}");
}
}
I put it on the Dialogue Manager GameObject.
If you don't need to carry save system data across scenes, it's fine to use SceneManager.LoadScene. Otherwise you will need to either use SaveSystem.LoadScene() or call a couple of methods before and after loading scenes: How To: Change Scenes With Save System