Object ref not set to an instance of an object

Hey there. Just finished this lecture and at 9mins we go into the SavingWrapper.cs script and change the LoadLastScene code to

        private IEnumerator LoadLastScene()
        {
            yield return GetComponent<SavingSystem>().LoadLastScene(defaultSaveFile);
            Fader fader = FindObjectOfType<Fader>();
            fader.FadeOutImmediate();
            yield return fader.FadeIn(fadeInTime);
        }

Sam says to change this so we don’t try to access the fader before we have spawned it in Awake. But this change is giving Unity the new error of.

Now in the Fader.cs script we have on line 17

I checked the code on github and it looks like we change this further in lessons ahead. The LoadLastScene section is completely different.

So… do I just proceed and ignore this for now? Or can (should) this be changed to something now to get rid of the error? I don’t know what it should be changed to, to fix the error condition.

Many Thanks. :smiley:

It looks to be a race condition? The CanvasGroup is null.
They both get called in awake.
In SavingWrapper;

        void Awake()
        {
            StartCoroutine(LoadLastScene());
        }

In Fader;


        private void Awake()
        {
            canvasGroup = GetComponent<CanvasGroup>();
        }

Not sure if it will introduce other problems,
but you could try to call the corooutine from start instead of awake.

Or to just test you could before “fader.FadeOutImmediate();”
put like;

yield return new WaitForSeconds(0.1f);

If its gone then you know whats the problem at least.

That is the eventual solution, along with ensuring that canvasGroup=GetComponent<CanvasGroup>(); is in Fader.Awake();

Awesome. Thank you so much for the reply. I’m going to try your solution and see. It does look like this will get fixed in a later class.

Thank you so much again. :slight_smile:

I moved
canvasGroup=GetComponent<CanvasGroup>();
in Fader.sc to Awake instead of Start and that fixed the problem.

I tried
yield return new WaitForSeconds(0.1f);
in SavingWrapper.cs just above the
fader.FadeOutImmediate();
and I was getting a warning error but I didn’t get a screen shot of it. I tried to replicate the error to get the warning text, but now it seems to be fixed after changing everything back and doing it again. Both solutions can fix the error now.

Thanks so much for the help guys. :rainbow: :rainbow: :star:

Do the first solution tho, the waitforseconds i said was just to do some testing for you to find it.
thats not really a good solution,

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms