Don't print the message! and not spawn in correct spawn point

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.

A silly little rule that DontDestroyOnLoad has is that it cannot be called on a child object, it must be called on an object in the root of the heirarchy. There is actually a fairly good reason for this… as you’re wanting the Portal to be DontDestroyOnLoad, but you don’t want the rest of the children done that way.

Ordinarily, we encourage you to use GameObjects as “folders” in the Heirarchy, put all your environment in a GameObject, put all your enemies in a GameObject, etc. Naturally, our first instinct is to put the Portals inside a GameObject as well to keep them altogether. Unfortunately, it doesn’t work because of this silly little rule of Unity.

Move your portals to the root of the heirarchy and you should be right as rain.

1 Like

Thanks Brian, I was organized it on folders and now its ok, I’m needing to investigate now about the spawn point that don’t working correctly, yet.

best regards
Carlos

What is happening with the Spawn point?

  • What do you expect to happen?
  • What actually happens? Be as specific as possible.

Hi Brian,

After back the Portals to the root, the player aren`t spawned in the correct portal, but, I did a new check on the code and found little change that I tryed, after fix it like the original on the github is working 100%.

many thanks for support me!

regards
Carlos

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