Thanks for reply.
I got no error message in console .
This is my Portal.cs
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.AI;
using RPG.Control;
namespace RPG.SceneManagement
{
public class Portal : MonoBehaviour
{
enum DestinationIdentifier
{
A, B, C, D, E
}
[SerializeField] int sceneToLoad = -1;
[SerializeField] Transform spwanPoint;
[SerializeField] DestinationIdentifier destination;
[SerializeField] float fadeOutTime = 1f;
[SerializeField] float fadeInTime = 2f;
[SerializeField] float fadeWaitTime = 0.5f;
private void OnTriggerEnter(Collider other)
{
if (other.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);
// Svae Current Level
SavingWrapper savingWrapper = FindObjectOfType<SavingWrapper>();
yield return fader.FadeOut(fadeOutTime);
savingWrapper.Save();
yield return SceneManager.LoadSceneAsync(sceneToLoad);
PlayerController newPlayerController = GameObject.FindWithTag("Player").GetComponent<PlayerController>();
newPlayerController.enabled = false;
savingWrapper.Load();
Portal otherPortal = GetOtherPortal();
UpdatePlayer(otherPortal);
savingWrapper.Save();
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.transform.position = otherPortal.spwanPoint.position;
player.transform.rotation = otherPortal.spwanPoint.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;
}
}
}