Issue with Prefab Portal not spawning correctly after switching scenes

First off! This has been a lot of fun and this is my first real stumbling block :slight_smile:

I have ran into an issue when I run back into a portal where the variable spawnPoint of Portal has not been assigned . You probably need to assign it in the inspector blah blah…

It is assigned in the inspector. I have tried deleting, reverting, reapplying prefabs and it only happens when I try to go backwards in the scene 1 to 0.

Any thoughts?

So it sounds like the spawnPoint in the Portal of scene 1 is set correctly, but the spawnPoint of the Portal in scene 0 isn’t.

Thanks for the response. The portal seems to work in Scene 0, but scene 1 is a different story. The spawn point is set in the inspector. I am at a loss of what the issue is since the console is not offering any good suggestions.

The spawn point is definitely assigned in Scene 1, but that means that when you transition from scene 0 to scene 1, a spawn point is found and the transition works…
If the error is popping up when you transition to scene 0, then the missing reference is in the portal in scene 0.

I was able to recreate the bug. If I start on scene 1 (not 0) the issue happens, but if I start gameplay on scene 0 then I have no issue. Is this something to do with the for each loop?

Are you saying that if you start in scene 0 then you can cross to scene 1 ok, and then back to scene 0 ok?

Correct.

Just not the other way around. If I start on 1 → 0 → 1 thats when the spawnpoint gives the error.

Can you post the screenshot of the selected portal in scene 0? (with the inspector, like you did for scene 1)

A couple things to check, just to make sure we’re not missing anything… In the little search box above the line “Sandbox” in scene one, type Portal. Everything should gray out and you should only see one Portal in the inspector.
If you find an extra portal, delete it, and try everything again. In a later lecture we’ll deal with multiple portals (using an identifier) but we’re not using those quite yet.

If not, then something else is going on. Post your Portal.cs script, and don’t forget to format it using the </> button at the top of the reply box.

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

namespace RPG.SceneManagment

{
    public class Portal : MonoBehaviour
    {

      [SerializeField] int sceneToLoad = -1;
      [SerializeField] Transform spawnPoint; 
      private void OnTriggerEnter(Collider other)
      {
         if (other.tag == "Player")
         {
           StartCoroutine(Transition());
         }
      }

      private IEnumerator Transition()
      {
          DontDestroyOnLoad(gameObject);
           yield return SceneManager.LoadSceneAsync(sceneToLoad);
           Portal otherPortal = GetOtherPortal();
           UpdatePlayer(otherPortal);
           Destroy(gameObject);
      }

      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;

           return portal;
         }

         return null;
      }
    }    
}

Everything looks right, which is quite odd…

Try deleting the Portal in scene 0 and recreating it.

It only triggers when you are going back through the portal. The spawnpoint is not being saved on the 2nd time through. This is so weird

Hmm. I’ll need to take a closer look.
Zip up your project and upload it to https://gdev.tv/projectupload Don’t forget to remove the Library folder to save space.

That one was a little harder to debug than I thought it would be.
I could tell that the portal script was picking up a portal other than the one in the scene, which was extremely odd, so I added a Debug in GetOtherPortal()

Debug.Log($"found portal {portal.name}");

The result I got was unexpected, it sad “Found Portal saving”

As it turns out, in your PersistentObjectPrefab, in the saving object there is a Portal component. Remove this and you should be good to go.

Thank you that worked. There must of been a reason why I threw the portal.cs in to the prefab… Just cant remember lol. Thanks again!

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

Privacy & Terms