Player not spawning at spawnPoint.position

I have copied and pasted the code (aside from a few irrelevant bits) from the lecture and I’m still getting this issue. There are no error messages in my console. What ends up happening after teleporting from my first scene to my second is my player does in fact teleport to the spawnpoint as it should, but only for a split second. After that, it teleports to the player to wherever I placed him in the scene before I pressed play, almost as if that positioning is overriding my code somehow. This only happens for my second scene. My first scene has no issues when trying to teleport back to that area. All of the enums are in place and are matched up as they should be. I did try to implement the player.GetComponent<NavMeshAgent>().Warp() function but I ended up getting the same result. Just figured I’d throw that out there to remove some steps in the troubleshooting process. I haven’t been able to find anyone who has this specific issue, but here is my code:

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

namespace RPG.SceneManagement
{
    public class Portal : MonoBehaviour
    {
        enum DestinationIdentifier
        {
            A, B, C, D, E
        }

        [SerializeField] Transform spawnPoint;
        [SerializeField] DestinationIdentifier destination;
        [SerializeField] int sceneToLoad = 1;

        void OnTriggerEnter(Collider other)
        {
            if (other.gameObject.CompareTag("Player"))
            {
                StartCoroutine(Transition());
            }
        }

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

            //DontDestroyOnLoad() only works on root game objects, meaning the game object this function is called on cannot be parented under any objects.
            DontDestroyOnLoad(gameObject);
            yield return SceneManager.LoadSceneAsync(sceneToLoad);

            Portal otherPortal = GetOtherPortal();
            UpdatePlayer(otherPortal);
            
            Destroy(gameObject);
        }

        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;
        }

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

                return portal;
            }
            return null;
        }
    }
}

here’s a video showing the issue I’m having in a small example scene I made.

I made a temporary solution in the meantime. It spits out a NullReferenceException error but it’s not game breaking. It’s just annoying in the console. Here’s what I made as a change:

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

            DontDestroyOnLoad(gameObject);
            yield return SceneManager.LoadSceneAsync(sceneToLoad);

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

            while (transform.position != otherPortal.transform.position)
            {
                yield return new WaitForSeconds(0.025f);
                transform.position = otherPortal.transform.position;
                Destroy(gameObject);
            }
        }

As you can see, I had the coroutine wait for a bit and then set the position a second time after waiting. It’s not a true fix, but it’s sweeping the problem under the rug until I get some working solution. For now, this will do.

I’m not at all sure what’s going on here, and even less sure of why the temporary solution solution would work…

Can you post a screenshot of the Portal’s inspector as well as a screenshot of the player’s inspector (in the case of hte player, you can collapse the components, I’m interested in seeing what the components are on the player.

All of the components on the player and portal (to my knowledge) are identical to what’s shown in the videos. I even went as far as messing with the size of the box collider to be identical to what is shown. I was really desperate to just simply understand what’s even happening haha. Here are some screenshots(player in scene, portal in scene, and portal prefab):



Ok, I think it might be easier to see what’s going on with a look at the project.
zip up your project (remove the Library folder) and upload it to https://gdev.tv/projectupload and I’ll see if I can nail down why this behaviour is emerging.

Thank you so much. I’ll send that over right now.

Umm… so now that I’m changing the code back to before I added that pseudo fix (for bug reproduction purposes), it’s now working flawlessly. This could be temporary, however, so I’m still going to send you the project in case you want to look something over. I’m experiencing no issues, though, so that’s super weird.

I meant the actual project directory (the directory you’d select when opening a project in Unity), not the built executable. I can’t explore the project structure or code with the exe.

That would make a lot more sense. Not sure at all what I was thinking. Sending that over in a sec

Sorry about the delay on this one. It’s been a busy week, and this one was “patched”, so a lower priority.
So the first thing I noticed when I loaded the first scene was that the persistent objects were present in the scene file. This is, ultimately, the root cause of your issue. The persistentObjects should only be in the PersistentObjectsPrefab, which should be loaded with the spawner in the Core Object.

It may have been a result of the import, but the core in both scenes had this field left blank, but this was largely masked by the presence of the items in the first scene.

So what happens is that we have a reference to the SavingSystem right up until the moment that the new scene is loaded… and at that point, the SavingSystem, SavingWrapper, and Fader are gone, just like that.
From that point, behaviour can get weird.

Removing the Persistent Objects from the scene file in the first scene, and adding the prefab to the core in both scenes resolved the error completely.

1 Like

Thank you so much for your reply!! I totally understand and that’s what I figured happened anyways. I cannot believe I let that happen right under my nose! I honestly forgot we had that persistent objects spawner script on core, otherwise I would’ve known. Thank you so much, and now that I applied the prefab to the serialized field in the script after deleting it in my first scene, the game works flawlessly.

1 Like

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

Privacy & Terms