[HOWTO] How To: Prevent Scene Changes From Incrementing Quests
Posted: Wed Apr 15, 2020 8:41 am
Quests commonly use Increment On Destroy components to increment quest counter variables.
Increment On Destroy listens for its GameObject to be destroyed or disabled. When you change scenes, Unity destroys all GameObjects in the old scene. This will cause the Increment On Destroy component to increment the counter.
To address this, change scenes using the LoadLevel() sequencer command or PixelCrushers.SaveSystem.LoadScene() C# method. Or you can configure a UnityEvent in the inspector to call SaveSystemMethods.LoadScene. For example, you could add a SaveSystemMethods component to a UI button and configure the button's OnClick() UnityEvent to call the SaveSystemMethods component's LoadScene method. You might want to set up the Dialogue System's integrated save system while you're at it, if you haven't already.
When you change scenes this way, it warns Increment On Destroy components that they're about to be destroyed by a scene change, so they shouldn't increment their counters. If you don't want to use any of those ways to change scenes, you can call PixelCrushers.SaveSystem.BeforeSceneChange() before changing scenes. This will also warn Increment On Destroy components. If you don't want to set up the save system, you can call PixelCrushers.DialogueSystem.PersistentDataManager.LevelWillBeUnloaded(), which will also warn Increment On Destroy components.
This also applies to Destructible Saver and Persistent Destructible components, which also listen for their GameObjects to be destroyed or disabled.
Increment On Destroy listens for its GameObject to be destroyed or disabled. When you change scenes, Unity destroys all GameObjects in the old scene. This will cause the Increment On Destroy component to increment the counter.
To address this, change scenes using the LoadLevel() sequencer command or PixelCrushers.SaveSystem.LoadScene() C# method. Or you can configure a UnityEvent in the inspector to call SaveSystemMethods.LoadScene. For example, you could add a SaveSystemMethods component to a UI button and configure the button's OnClick() UnityEvent to call the SaveSystemMethods component's LoadScene method. You might want to set up the Dialogue System's integrated save system while you're at it, if you haven't already.
When you change scenes this way, it warns Increment On Destroy components that they're about to be destroyed by a scene change, so they shouldn't increment their counters. If you don't want to use any of those ways to change scenes, you can call PixelCrushers.SaveSystem.BeforeSceneChange() before changing scenes. This will also warn Increment On Destroy components. If you don't want to set up the save system, you can call PixelCrushers.DialogueSystem.PersistentDataManager.LevelWillBeUnloaded(), which will also warn Increment On Destroy components.
This also applies to Destructible Saver and Persistent Destructible components, which also listen for their GameObjects to be destroyed or disabled.