Hello everyone, unfortunately a problem arises for me.
DontDestroyOnLoad works only for root GameObjects or components on root GameObjects.
UnityEngine.StackTraceUtility:ExtractStackTrace ()
RPG.SceneManagment.Portal/d__3:MoveNext () (at Assets/Script/SceneManagment/Portal.cs:24)
UnityEngine.MonoBehaviour:StartCoroutine (System.Collections.IEnumerator)
RPG.SceneManagment.Portal:OnTriggerEnter (UnityEngine.Collider) (at Assets/Script/SceneManagment/Portal.cs:18)
Yet it is a GameObject with a parent object. Or am I misunderstanding something?
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace RPG.SceneManagement
{
public class Portal : MonoBehaviour
{
[SerializeField] int sceneToLoad = -1;
[SerializeField] Transform spawnPoint;
private void OnTriggerEnter(Collider other) {
if (other.tag == "Player")
{
StartCoroutine(Transition());
}
}
private IEnumerator Transition()
{
DontDestroyOnLoad(gameObject);
yield return SceneManager.LoadSceneAsync(sceneToLoad);
print("Scene Loaded");
Portal otherPortal = GetOtherPortal();
UpdatePlayer(otherPortal);
Destroy(gameObject);
}
private void UpdatePlayer(Portal otherPortal)
{
GameObject player = GameObject.FindWithTag("Player");
player.transform.position = otherPortal.spawnPoint.position;
player.transform.rotation = otherPortal.spawnPoint.rotation;
}
private Portal GetOtherPortal()
{
foreach (Portal portal in FindObjectsOfType<Portal>())
{
if (portal == this) continue;
return portal;
}
return null;
}
}
}
Can anyone help me with this?
Oh I just see my predecessor had the same problem only I am not aware of how I can solve it?