Now I integrated the saving system and the error still exists.
I am also of the opinion that the scene is completely reloaded and that such an error should not actually occur.
Here is my Portal.cs file, I have this project in a private GitHub repository, if you want i can make the repository public and send the link to it.
For this project I use Unity 2021.3.1f1
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.SceneManagement;
namespace RPG.SceneManagement
{
public class Portal : MonoBehaviour
{
enum DestinationIdentifier
{
A, B, C, D, E
}
[SerializeField, Range(-1, 100)] int sceneToLoad = -1;
[SerializeField] Transform spawnPoint;
[SerializeField] DestinationIdentifier destination;
[SerializeField] float fadeOutTime = 0.5f;
[SerializeField] float fadeInTime = 1f;
[SerializeField] float fadeWaitTime = 0.5f;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
StartCoroutine(Transition());
}
}
private IEnumerator Transition()
{
if(sceneToLoad < 0)
{
Debug.LogError("Scene to load not set");
yield break;
}
DontDestroyOnLoad(gameObject);
Fader fader = FindObjectOfType<Fader>();
yield return fader.FadeOut(fadeOutTime);
SavingWrapper savingWrapper = FindObjectOfType<SavingWrapper>();
savingWrapper.Save();
yield return SceneManager.LoadSceneAsync(sceneToLoad);
savingWrapper.Load();
Portal otherPortal = GetOtherPortal();
UpdatePlayer(otherPortal);
yield return new WaitForSeconds(fadeWaitTime);
yield return fader.FadeIn(fadeInTime);
Destroy(gameObject);
}
private void UpdatePlayer(Portal otherPortal)
{
GameObject player = GameObject.FindWithTag("Player");
player.GetComponent<NavMeshAgent>().enabled = false;
player.GetComponent<NavMeshAgent>().Warp(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;
if (portal.destination != destination) continue;
return portal;
}
return null;
}
}
}