NullReference Exception with portal only working when delete and replace core

Hi everything related to the portals and scene transition has been working fine for me and at the time of writing this, I have been working through the saving section.

Now suddenly I’m experiencing a very strange null reference exception which is making no sense to me because I can’t identify what is causing this in the code or in any of the object settings.

So I tried deleting the core Gameobject and replaced the prefab and then everything works fine. However whenever I quit the project(close unity completely) and then relaunch and go back into playmode I get the following error again:

NullReferenceException: Object reference not set to an instance of an object
RPG.SceneManagement.Portal+<Transition>d__10.MoveNext () (at Assets/Scripts/SceneManagement/Portal.cs:52)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <823fb226a3f9439cb41fdcb61f9c86a1>:0)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
RPG.SceneManagement.Portal:OnTriggerEnter(Collider) (at Assets/Scripts/SceneManagement/Portal.cs:36)

The first line it mentions in the error log on line 52:

    IEnumerator Transition()
        {
            if (_sceneToLoad < 0)
            {
                Debug.LogError("Scene to load not set");
                yield break;
            }
            SavingWrapper savingWrapper = FindObjectOfType<SavingWrapper>();

            Object.DontDestroyOnLoad(gameObject);
            savingWrapper.Save();

            yield return _fader.FadeOut(_fadeOutTime);
            yield return SceneManager.LoadSceneAsync(_sceneToLoad);
            savingWrapper.Load();

            Portal otherPortal = GetOtherPortal();
            UpdatePlayer(otherPortal);

            yield return new WaitForSeconds(_fadeWaitTime);
            yield return _fader.FadeIn(_fadeInTime);

            Destroy(gameObject);
        }

The specific line is:
yield return _fader.FadeOut(_fadeOutTime);

The next again in portal for line 36 is the startcoroutine call in:

 private void OnTriggerEnter(Collider other)
        {
            if (other.tag == "Player")
            {
                StartCoroutine(Transition());
            }
        }

Again this is very odd because it was working fine previously and it works after deleting and replacing the core prefab and then going into play mode, but then the error occurs when relaunching the entire project.

I’m using unity 2021.3.1 LTS if that’s related.

How are you assigning _fader?

Just to make sure, assign the _fader variable immediately before the first ield return

_fader = FindGameObjectOfType<Fader>();

I’m assigning it in awake:

private void Awake()
        {
            _fader = FindObjectOfType<Fader>();
        }

And sometimes this bug occurs within a minute or two after being in the scene so it will have had ample time to be assigned.

Is there any chance you have a Fader in the scene file? It should only be in the PersistentObjectsPrefab. If there’s one in the scene file it will always be the one found by FindObjectOfType because DontDestroyOnLoad objects are technically in a different scene and the Find methods go through that scene only after they have searched the entire scene.

Does the bug replicate if you assign the fader immediately before using it in the Transition coroutine?

1 Like

I’ve just changed the instantiation of fader just before transition like you suggested:

private void OnTriggerEnter(Collider other)
        {
            if (other.tag == "Player")
            {
                _fader = FindObjectOfType<Fader>();
                StartCoroutine(Transition());
            }
        }

And now it appears to be working correctly, I don’t understand why moving it from awake to there fixed it though.

I didn’t find another fader in the scene but is the reason your suggestion fixed it is because in awake it will have found the portal from the previous scene?

Although this bug did sometimes happen without scene transitioning as well.

Theoretically, it should be fine in Awake(), unless there is no save file in which case the scene isn’t reloaded and it’s possible for the _fader to be initialized before the PersistentObjectsPrefabSpawner spawns the prefab. This is a case of since it’s a one off, I’ve never bothered caching the Fader, just getting it right before it’s first use.

1 Like

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

Privacy & Terms