Hi guys,
I`m can’t discover what are happens in my code, is the same that the github but don’t work…
the error is:
DontDestroyOnLoad only works for root GameObjects or components on root GameObjects.
UnityEngine.Object:DontDestroyOnLoad(Object)
RPG.SceneManagement.d__3:MoveNext() (at Assets/codigos/SceneManagement/Portal.cs:22)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
RPG.SceneManagement.Portal:OnTriggerEnter(Collider) (at Assets/codigos/SceneManagement/Portal.cs:17)
the problem is on the portal between scenes.
my portal.cs code:
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.AI;
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);
Portal otherPortal = GetOtherPortal();
UpdatePlayer(otherPortal);
Destroy(gameObject);
private void UpdatePlayer(Portal otherPortal)
{
GameObject player = GameObject.FindWithTag(“Player”);
player.GetComponent().Warp(otherPortal.spawnPoint.position);
player.transform.rotation = otherPortal.spawnPoint.rotation;
}
private Portal GetOtherPortal()
{
foreach (Portal portal in FindObjectsOfType())
{
if (portal == this) continue;
return portal;
}
return null;
}
}
}
thank you.