Page 1 of 1
PixelCrushers.SaveSystem.sceneLoaded Called multiple times
Posted: Wed Apr 16, 2025 3:56 am
by robscatch
Hi,
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.
Code: Select all
public void StartMicroGames()
{
PixelCrushers.SaveSystem.LoadAdditiveScene(MiniGameSceneName);
PixelCrushers.SaveSystem.sceneLoaded += OnSceneLoaded; // Subscribe to the scene loaded event
}
private void OnSceneLoaded(string sceneName, int sceneIndex)
{
//Do stuff
}
Thanks for the help
Re: PixelCrushers.SaveSystem.sceneLoaded Called multiple times
Posted: Wed Apr 16, 2025 8:09 am
by Tony Li
Hi,
Is it possible that StartMicroGames() gets called multiple times over the course of the game? Try this instead:
Code: Select all
PixelCrushers.SaveSystem.sceneLoaded -= OnSceneLoaded;
PixelCrushers.SaveSystem.sceneLoaded += OnSceneLoaded;
This should keep StartMicroGames() from hooking into sceneLoaded more than once.
Re: PixelCrushers.SaveSystem.sceneLoaded Called multiple times
Posted: Fri Apr 18, 2025 2:06 am
by robscatch
Hi,
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.
Code: Select all
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.
Code: Select all
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
Code: Select all
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
};
}
Re: PixelCrushers.SaveSystem.sceneLoaded Called multiple times
Posted: Fri Apr 18, 2025 8:17 am
by Tony Li
Hi,
I just tested this script and confirmed that SaveSystem.sceneLoaded will only be called once per scene load:
Code: Select all
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
Since you're also loading scenes additively, use
SaveSystem.LoadAdditiveScene and UnloadAdditiveScene.
Re: PixelCrushers.SaveSystem.sceneLoaded Called multiple times
Posted: Fri Apr 18, 2025 8:31 pm
by robscatch
Thanks for the help. I'll try to see what's going on with my scripts.
Re: PixelCrushers.SaveSystem.sceneLoaded Called multiple times
Posted: Fri Apr 18, 2025 8:56 pm
by Tony Li
Sounds good. If you can't find the issue, feel free to send a reproduction project to tony (at) pixelcrushers.com