Unable to control Player after spawn from second scene

I have created the player spawn points
They work in between scenes when I start from the first original scene

However, i have an issue if i start playing from my second scene - go to the first(orignal) scene - then return to the second scene where i started.
When returned to to the second scene, I am unable to control my player for a while then he barrels off to the other side of the world

So in the transition from Scene 1 to Scene 2, you wind up back in Scene 2?

It sounds like you’re SpawnPoint on the Scene 1 Portal is too close to the Portal itself, which would cause the player to rebound back to the second scene.

It’s possible the second set of behaviors you’re describing are caused by the conflict of the two scenes trying to load/restore at the same time, but I can’t be sure. I haven’t seen the player deciding to run off to the other side of the world without specific input.

Hi Brian,

Thanks for getting back to me.

not quite.

If i start in scene one, i can portal fine to scene 2 and portal back to scene 1 infinitely and with no issues

But if i start the game in scene 2 and then go to scene 1 successfully, then return to scene 2 with the portals I no longer have control of my player. he just stands in scene 2 at the spawn point.

I have experimented with removing the spawn point (just loading the scene) and the same issue occurs.

It is working fine generally - it is just this specific case i am getting this issue with losing control

That is odd indeed.
Are you getting any errors in the console? Adding to that, are you getting any -=warnings=- in the console when switching scenes?

Things to check:
Make sure that SavingWrapper, SavingSystem, and Fader all only exist within the PersistentObjectPrefab, and nowhere within any of the scene files themselves (when playing, there should only be a copy of the PersistentObjects in the “DontDetroyOnLoad” Scene.

Make sure all of your Portals are at the root of the heirarchy, with no parents, not under a parent GameObject.

Paste in your Portal.cs if neither of those things fixes it.

Hi Brian,

I am actually not even at the saving part yet - i have no fader etc.

All the portals are at the root and I am getting no warnings at all either.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.SceneManagement;

namespace RPG.SceneManagement
{
    public class Portal : MonoBehaviour
    {
        [SerializeField] string sceneName;
        [SerializeField] Transform spawnPoint;
        GameObject player;

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

        }
        IEnumerator Transition()
        {
            DontDestroyOnLoad(gameObject);
            yield return SceneManager.LoadSceneAsync(sceneName);
        
            print("sceneloaded");

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

            Destroy(gameObject);
        }

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

                return portal;
            }
            return null;
        }

        private void UpdatePlayer(Portal otherPortal)
        {
            player = GameObject.FindGameObjectWithTag("Player");
            player.GetComponent<NavMeshAgent>().Warp(otherPortal.spawnPoint.transform.position);
          //  player.transform.position = otherPortal.spawnPoint.transform.position;
          //  player.transform.rotation = otherPortal.spawnPoint.transform.rotation;

        }

    }
    
}

Thanks again for the help

There is nothing in the scripts that I can think of that would be causing this at this point in the game, especially without throwing errors. I’ll need to take a closer look.

Zip up your project and upload it to https://gdev.tv/projectupload
Be sure to remove the Library folder to conserve space.

thank you for taking the time
I am currently uploading a copy of my files there
(I wasn’t sure if deleting my library would create any issues for me so i just made a copy and deleted the folder from there)

This is where i am up to on my course

It did not appear to go through

i keep getting timed out on while uploading
I have it on GitHub if i can send you that?

Go ahead and send me the Gihub link

Thank you : https://github.com/Liamcodeshard/07.03.2022-Sandbox.git

It is on the PortalProblems branch

I have set it up to recreate the problem easiest and so you start in scene2 and the white boxes are the portals.

If you play the game from the second scene and then walk into the portals (white boxes) twice, i.e. when each scene starts, you will recreate the problem

Essentially, if i start the game in the second scene then go to the first, and return to the second scene, i lose control of the player.

FYI Sometimes he will by chance run into the next portal - when i return AGAIN to the first scene, control is restored.
The portals work fine otherwise

First of all, excellent scene layout, and I really liked the cutscene. Unfortunately, I can’t seem reproduce the issue, even when running to the portal before the fader is finished…

That is frustrating!

In the version you have, are you able to get to the portal branch?
I am not fully sure how GitHub works when downloading from your end.

And when pressing play, did you start in the woodland scene or the house scene?

I tried it starting in all three of the Sandbox scenes. Once, I got a null reference error, but was still able to continue.
Loaded again, and spent 15 minutes running from scene to scene with no issues.

Thank you for that - what would you suggest for me?

I’m honestly not sure. What you’re describing sounds like a situation that can arise once we put the Fader in the PersistentObjectsPrefab (lecture Better Than a Singleton), but… you’re not there yet, the Fader is in each scene atm.
Without being able to reproduce it on my end, I’m stuck when it comes to finding a solution…
You might try deleting the existing portal (seems like from your description, Portal 2) and re-creating it.

Privacy & Terms