I know Brian has done some update transition videos for this course for Unity 6, but I thought I would post a version of the SceneManagement.Portal class for Unity 6. This is updated to remove deprecated functions and a few other tweaks for de-conflicting with the NavMeshAgent.
public class Portal : MonoBehaviour
{
enum DestinationIdentifier
{
A, B, C, D, E
}
[SerializeField] int sceneToLoad = -1;
[SerializeField] Transform spawnPoint;
[SerializeField] DestinationIdentifier destination;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
if (sceneToLoad < 0)
{
Debug.LogError("Scene to load not set");
return;
}
StartCoroutine(Transition());
}
}
private IEnumerator Transition()
{
DontDestroyOnLoad(gameObject);
yield return SceneManager.LoadSceneAsync(sceneToLoad);
Portal otherPortal = GetOtherPortal();
UpdatePlayer(otherPortal);
Destroy(gameObject);
}
private void UpdatePlayer(Portal otherPortal)
{
if (otherPortal == null)
{
Debug.LogError("Other portal not found");
return;
}
GameObject player = GameObject.FindWithTag("Player");
player.GetComponent<NavMeshAgent>().Warp(otherPortal.spawnPoint.position);
player.transform.rotation = otherPortal.spawnPoint.rotation;
}
private Portal GetOtherPortal()
{
foreach (Portal portal in FindObjectsByType<Portal>(FindObjectsSortMode.None))
{
if (portal == this) continue;
if (portal.destination != destination) continue;
return portal;
}
return null;
}
}