Player Spawn Point

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?

here is a Video.

The issue is that your Portal isn’t a root object in the scene. The game is trying to tell you (in a warning, not an error, like it should be) that it can’t move it to the DontDestroyOnLoad section because it only works on root objects. Move the Portal out of the Core (probably a bad place for it anyways) so that it has no parent, and you should be good to go.

1 Like

I’ll try that, thank you.

Hat funktioniert ich danke dir!

You’re welcome.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms