How exactly do portals match up?

Per the video, it would appear that you link portals by matching the scene number and destination, but it does not seem to work that way consistently. Here is an representation of three scenes from my project, which I am trying to connect with portals:

For some reason, when I try to load scene 3, the portal in scene 2 fades out, produces a Missing Reference Exception, and reloads scene 2 (not 3).

Occasionally the error is something different, such as “null reference” or “can not instantiate”, but I dont have screens for those because I could not consistently repeat those fact patterns.

Some other notes:

  • I am using the same player prefab in all scenes, as part of the Core prefab
  • My persistent object loader does not delete the “Scene 3 Destination A” portal from the hierarchy and instead creates clones of itself (because the error occurs on the line with “newPlayerController.enabled = true”)

Here is my portal code:

public class Portal : MonoBehaviour
        enum DestinationIdentifier
            A, B, C, D, E

        [SerializeField] int sceneToLoad = -1;
        [SerializeField] Transform spawnPoint;
        [SerializeField] DestinationIdentifier destination;
        [SerializeField] float fadeOutTime = 1f;
        [SerializeField] float fadeInTime = 2f;
        [SerializeField] float fadeWaitTime = 0.5f;

        private void OnTriggerEnter(Collider other)
            if (other.tag == "Player")

        private IEnumerator Transition()
            if (sceneToLoad < 0)
                Debug.LogError("Scene to load not set.");
                yield break;


            Fader fader = FindObjectOfType<Fader>();
            SavingWrapper savingWrapper = FindObjectOfType<SavingWrapper>();
            PlayerController playerController = GameObject.FindWithTag("Player").GetComponent<PlayerController>();
            //Remove control
            playerController.enabled = false;

            yield return fader.FadeOut(fadeOutTime);


            yield return SceneManager.LoadSceneAsync(sceneToLoad);
            PlayerController newPlayerController = GameObject.FindWithTag("Player").GetComponent<PlayerController>();
            //Remove control
            newPlayerController.enabled = false;

            Portal otherPortal = GetOtherPortal();


            yield return new WaitForSeconds(fadeWaitTime);

            //Restore control
            newPlayerController.enabled = true;

        private void UpdatePlayer(Portal otherPortal)
            GameObject player = GameObject.FindWithTag("Player");
            player.GetComponent<NavMeshAgent>().enabled = false;
            player.transform.position = otherPortal.spawnPoint.position;
            player.transform.rotation = otherPortal.spawnPoint.rotation;
            player.GetComponent<NavMeshAgent>().enabled = true;

        private Portal GetOtherPortal()
            foreach (Portal portal in FindObjectsOfType<Portal>())
                if (portal == this) continue;
                if (portal.destination != destination) continue;

                return portal;

            return null;

Has anyone else had this issue come up? I’m mostly not sure why its an issue with some scenes but not with others.

Found one more weird bug, if I keep trying to force my way through the non-functioning portal, eventually it does load scene 3, but it spawns me in a random spot. The exception error only gets logged one time.

Turns out the problem was the collider on the spawn point. When I was tweaking the portal, the collider became slightly overlapped with the spawn point, causing the player to instantly go back to the last scene.

Good job finding that. Those types of issues are difficult to diagnose, as the code is correct. That’s part of the fun challenge of Unity, as issues can exist either in code, or the Unity Editor, or both.

1 Like

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