Had issue with null references and animations not playing (fixed)

Hello,

I was having an issue where at first when I went through a portal and it faded out, I would get a null reference regarding my warp to location when restoring the player’s location. After testing I found that it was because the navMeshAgent was null, it wasn’t loading in before the saving system tried to move the player. After a simple if statement making sure that the navmesh was present before warping the player I thought I was clear. Next when we made Start load the scene to the saved scene, I had an issue where previously dead enemies were still standing, their health was 0 but the die animation wasnt triggering. Again testing found that the animator loads but there is a timing conflict with restore state.

In the end my solution was to go into SavingSystem.cs and under LoastLastScene I added a WaitForSeconds after the LoadSceneAsync and before RestoreState. Not sure how long lasting this fix is, and there is a half second jarring when the application loads but it works.

        public IEnumerator LoadLastScene(string saveFile)
        {
            Dictionary<string, object> state = LoadFile(saveFile);
            int buildIndex = SceneManager.GetActiveScene().buildIndex;
            if (state.ContainsKey("lastSceneBuildIndex"))
            {
                buildIndex = (int)state["lastSceneBuildIndex"];
            }
            yield return SceneManager.LoadSceneAsync(buildIndex);
            yield return new WaitForSeconds(0.5f); //added myself            
            RestoreState(state);
        }

This masks what’s really going on. The issue is that LoadSceneAsync returns immediately after all the Awakes have been called, but before the OnEnable and Start() methods have been called. In truth, you could replace this with

yield return new WaitTillEndOfFrame();

and it should work just fine.
The full solution involves make sure that all object initialization (caching references) happens in Awake(), and that references to external components happen after Awake();
The most common culprit is in Fader.cs, where the reference to the CanvasGroup is cached in Start(), but we fade in right after loading and restoring. (Moving canvasGroup=GetComponent(); to Awake() fixes that particular bug.

1 Like

İ had similliar issue(Animations doesnt work even at 0 health).İ’ve just done what you are saying – yield return new WaitTillEndOfFrame();
The thing is editor didnt allow me to write this line.After little reading unity doc. i found that i can use it as --> yield return new WaitForEndOfFrame(); i Added it as Mike says under the Saving System.cs it worked.Then i have second issue which is when i use portal back to first scene, animation didnt work again. To solve that i added to after LoadSceneAsync under the portal this.
—> yield return new WaitForEndOfFrame();
İ dont know is it good way or not but it worked.

Yes, it appears I made a typo on that one, sorry about that.
new WaitForEndOfFrame() works, all it’s doing is giving all the objects a chance to run OnEnable() and Start(). You could also use yield return null; (it will have the same effect)

In terms of it being a good way, as I said before, it’s masking the real problem, that references are being cached in Start() instead of Awake() (and yes, I know, in many of these cases we told you to cache a reference in Start()

Yes. Actually i saw that before lecture.İ’ve already changed it Start to Awake.
By the way thanks for responses Brian. İ’ve been seeing almost all problems on this course you are trying to solve them. İt wouldnt be that easy without you. You rock !

Privacy & Terms