Race Condition in Fader - another rename Start to Awake fix

I’ve noticed another race condition, this time in our Fader.cs. In particular, a null reference in Fader.FadeOutImmediate, invoked by SavingWrapper.LoadLastScene. This became a persistent issue for me when I added a few enemies to the Sandbox 2 scene, went through the portal from the main scene, and then quit. Playing the game from this save state would cause the exception and mis-load the scene, reverting to the original scene instead.

The fix, I think, is to change Start to Awake in Fader.cs so that the canvasGroup reference is properly obtained before FadeOutImmediate is invoked by SavingWrapper (which after this lecture now happens even earlier).

I notice by the time the course gets to the “Awake vs Start” video, this change has already been made, so maybe I missed it earlier, or it was changed behind-the-scenes. Either way, it’s eventually fixed.

Very much appreciated your post as I was seeing the exact same issue.

This particular issue has hung up many a student, I can assure you. It is mentioned, though I can’t quite point to where it is. As a rule of thumb:

Cache all references in Awake()
Subscribe to events on other objects in OnEnable()
Actually reference other objects in Start() or Update()