SaveSystem causing Awake() to be called multiple times

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
cptscrimshaw
Posts: 113
Joined: Sun Sep 20, 2020 8:21 pm

SaveSystem causing Awake() to be called multiple times

Post by cptscrimshaw »

Hi Tony,

I've run into a little issue that I think has probably been occuring for a while and just hasn't affected anything strangely until now. I'm using the PixelCrushers save system to save and load, and it appears to result in the Awake() method being called multiple times.

If I start from fresh (no save files), Awake() is called once. If I play the game in the editor, then quit, then play again, Awake is called 3 times, once before the save system loads, and twice after. Included is a screenshot of the console and here is just some super simple code I'm using in one of my classes:

Code: Select all

private void Awake()
{
    Debug.Log("Calling awake");
    SaveSystem.saveDataApplied += OnSaveDataApplied;
    EventHandler.ScenePortalTriggered += SaveBeforeChangingScenes;
}

void OnSaveDataApplied()
{
    Debug.Log("save data applied");
}
https://ibb.co/jMZpKKf

Any idea what could be causing this? There are no duplicates of this specific class, and the savesystem is on just one gameobject in the scene. In this specific case, there was no change in scene from the editor to play mode.
User avatar
Tony Li
Posts: 22050
Joined: Thu Jul 18, 2013 1:27 pm

Re: SaveSystem causing Awake() to be called multiple times

Post by Tony Li »

Hi,

Quick note first: If you're registering a handler for SaveSystem.saveDataApplied in Awake(), make sure to unregister in OnDestroy(). Another common place to register and unregister is OnEnable() and OnDisable().

---

Are you using an Auto Save Load component? If so, it may be starting the first scene, then seeing that there's a saved game, so it loads the loading scene and then the scene in which the game was saved -- even if it's the same scene that you started in. This is important to do because it resets the scene back to a clean state before applying the saved game data.

If you tick the Save System component's Debug checkbox, it will log a line like this when changing scenes:

Save System: Loading scene scene name

This might give you more context as to where the Awake() debug lines are coming from.

Note that if you're using a Standard Scene Transition Manager component with a loading screen scene, the Save System will not log that message for the loading screen scene.
Post Reply