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.