I’ll let you in on a dirty little secret… The Persistent Object Prefab is… Just another type of Singleton…
You can set the SavingSystem, SavingWrapper, and Fader as Singletons, if you wish. In fact, as we move forward, we never really mention the PersistentObjectSpawner again. It was a construct that Sam used to get away from the classic Singleton pattern.
Singletons can be a useful tool, but in classic OOP, there are many (like myself, before learning Unity, as a matter of fact) who feel that Singletons do a sort of end run around the entire paradigm of Object Oriented Programming. Of course, Unity’s architecture sort of pushes us into needing Singletons both because some things need to survive between scenes, and because we often need an efficient way to find that one thing.
If it helps for debugging, the issue you’re experiencing with the Fader not fading has three potential root causes (two generally yield an error message, and one a warning which should be an error message, which should help).
The first occurs when the game is first started and there is an existing save file. In this case, the issue is that the CanvasGroup is initialized in Start() instead of Awake().
The second occurs when travelling through a Portal. In the Coroutine, the Fader is assigned and told to FadeOutImmediate, so far, so good. Then after the scene transition, the Fader gets a null reference exception. This is usually caused because of forgetting to remove the PersistentObjectPrefab from the scene heirarchy after it’s been turned into a Prefab.
The third is caused if the Portal is not at the root of the heirarchy. Ordinarily, we recommend organizing scene objects, and instinctively putting all of the Portals under a master GameObject is something that makes sense. The issue there is that when you make a GameObject DontDestroyOnLoad, that GameObject cannot have a parent. There are two solutions for this:
- You can make sure all Portals are at the root of the Heirarchy (have no parent) or
- You can put this line immediately before the
DontDestroyOnLoad(gameObject);
transform.parent = null;