Both portal in scene 2 always going back to the start

Hi Guys,

Course RPG.combat (Scene Management (Cross scene references))

The issue I have is that trying to link my portals in the second scene to the corresponding ones in scene 1 but they both go back to the start.

this should go here

but goes here

I get no errors but here is my code.
using System;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.SceneManagement;

namespace RPG.SceneManagement


enum DestinationIdentifier


    A, B, C, D, E


public class Portal : MonoBehaviour


    [SerializeField] int sceneToLoad = -1;

    [SerializeField] Transform spawnPoint;

    [SerializeField] DestinationIdentifier destination;

    private void OnTriggerEnter(Collider other)


        if (other.tag == "Player")





    private IEnumerator Transition()


        if (sceneToLoad < 0)


            Debug.LogError("Scene to load not set");

            yield break;




        yield return SceneManager.LoadSceneAsync(sceneToLoad);


        Portal otherPortal = GetOtherPortal();




    private void UpdatePlayer(Portal otherPortal)


        GameObject player = GameObject.FindWithTag("Player");

        player.transform.position = otherPortal.spawnPoint.position;

        player.transform.rotation = otherPortal.spawnPoint.rotation;


    private Portal GetOtherPortal()


        foreach (Portal portal in FindObjectsOfType<Portal>())


            if (portal==this) continue;

            if (portal.destination != destination) continue;

            return portal;


        return null;




No errors, but I suspect there is a warning (yellow message) that in my opinion, Unity SHOULD be escalating to an error…
It should look something like

DontDestroyOnLoad only works for root GameObjects or components on root GameObjects

The problem is that your portals are neatly organized under an encapsulating GameObject. While this helps make the scene nice and tidy, and I recommend this for almost every other GameObject in the game, it won’t work with Portals (or any other GameObject that needs to be made DontDestroyOnLoad()).

So here’s specifically why this isn’t working:
When you call DontDestroyOnLoad(gameObject) in the transition coroutine, the Portal is left untouched, and the warning is raised – which is often ignored because most folks get sick of seeing all of the “variable blah is not being used” messages in their console burying the actual error messges, so we turn them off or just ignore them altogether.
Then the scene loads in LoadSceneAsync… at this point the Portal is destroyed, very quietly.
Control is returned to the coroutine, but since the Portal no longer exists, Unity just says “meh, nevermind” and goes about it’s business, not even leaving us a nice note like “Coroutine abandoned because underlying component was destroyed”.

1 Like

Hi Brian,
Thanks again.

Yeah thought I was being tidy took them out of the parent game object its come up another problem where the player on the return from A-A is being placed int the wrong place. I will give it a go on where to fix it.

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

Privacy & Terms