DontDestroyOnLoad Still Destroying Object

I have an empty game object that contains the two objects for my player as its children. I have a script that is on the empty parent that calls DontDestroyOnLoad, however I get the error: “DontDestroyOnLoad only works for root GameObjects or components on root GameObjects.”
I’ve tried putting DontDestroyOnLoad in Awake, but still nothing.

Here’s my script if it helps:

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System;

public class PlayerPresence : MonoBehaviour {

    public GameObject countdown;

    bool spawned;
    bool alive;
    bool sceneChangeTrigger;
    Scene currentScene;
    bool toggleable;

    private int lives;
    private int currentLevelChoice;

	// Use this for initialization

    void Start ()
    {
        currentScene = SceneManager.GetActiveScene();

        toggleable = false;

        sceneChangeTrigger = false;

        alive = true;

        spawned = false;

        lives = 3;

        GetComponentInChildren<Camera>().enabled = false;

        currentLevelChoice = GameObject.FindGameObjectWithTag("DontDestroy").GetComponent<HoldValues>().GetLevelChoice() + 3;

        Destroy(GameObject.FindGameObjectWithTag("DontDestroy"));

        DontDestroyOnLoad(this.gameObject);
    }

    // Update is called once per frame
	void Update ()
    {
        if (SceneManager.GetActiveScene().name == "SpinMap" ||
            SceneManager.GetActiveScene().name == "Shelter")
        {
            if (!sceneChangeTrigger)
            {
                sceneChangeTrigger = true;

                CountDown();
            }
        }

        if (Input.GetKeyUp(KeyCode.Return) && currentScene.name == "CharacterSelect")
        {
            SceneManager.LoadScene(currentLevelChoice);
        }

        currentScene = SceneManager.GetActiveScene();

        if (toggleable && currentScene.name == "CharacterSelect")
        {
            toggleable = false;

            ToggleMaterialChange();

            TurnOffFixRotation();
        }

        if(currentScene.name != "CharacterSelect" && !toggleable)
        {
            ToggleMaterialChange();

            TurnOnFixRotation();
            TurnOnMovement();

            toggleable = true;
        }

        if(!spawned && currentScene.buildIndex > 2)
        {
            spawned = false;

            Spawn();
        }
    }

    void Spawn()
    {
        if(lives != 0 && SceneManager.GetActiveScene().buildIndex > 2)
        {
            GameObject[] spawnPoints = GameObject.FindGameObjectsWithTag("Respawn");

            float shortestDistance = Vector3.Distance(Vector3.zero, spawnPoints[0].GetComponent<Transform>().position);

            for (int i = 1; i < spawnPoints.Length; i++)
            {
                if (shortestDistance > Vector3.Distance(Vector3.zero, spawnPoints*.GetComponent<Transform>().position) &&*

!spawnPoints*.GetComponent().GetState())*
transform.position = spawnPoints*.GetComponent().position;*
}

if(!spawnPoints[0].GetComponent().GetState())
transform.position = spawnPoints[0].GetComponent().position;
}
}

void TurnOffMovement()
{
GetComponentInChildren().enabled = false;
GetComponentInChildren().enabled = false;
}

void TurnOnMovement()
{
GetComponentInChildren().enabled = true;
GetComponentInChildren().enabled = true;
}

public void Destroy()
{
Destroy(this.gameObject);
}

public void ToggleMaterialChange()
{
ChangeMaterial[] enableds = GetComponentsInChildren();

foreach(ChangeMaterial mat in enableds)
{
if (mat.enabled)
mat.enabled = false;
else if (!mat.enabled)
mat.enabled = true;
}
}

void TurnOffFixRotation()
{
GetComponentInChildren().enabled = false;
}

void TurnOnFixRotation()
{
GetComponentInChildren().enabled = true;
}

public void CountDown()
{
StartCoroutine(StartGame());
}

IEnumerator StartGame()
{
GetComponentInChildren().enabled = true;

GameObject clone = Instantiate(countdown, countdown.GetComponent().position, Quaternion.identity) as GameObject;

clone.GetComponent().enabled = true;

int i = 5;
while (i < 5)
{
clone.GetComponent().text = i.ToString();

yield return new WaitForSeconds(1f);

i++;
}

clone.GetComponent().enabled = false;
}
}

It’s because your Game Object is child or another Game Object that isn’t been marked as Don’t Destroy On Load. The parent Game Object will be destroyed and also their childrens.

In order to preserve the Game Object you will have to make myGameObject.parent = null before reaload the new scene, or change your hierarchy to place it in the root.

+1 I’m having the same issue. On multiple scene reloads, if the object was a child of a parent on the first reload, but then has no parent on the second reload but still has DontDestroyOnLoad which was called in its Awake() function, it is destroyed on the second scene reload.

Try this 2D Roguelike 5 of 14 : Game Manager - YouTube

Maximetu, this is not what’s happening. My object has no parent and it has called DontDestroyOnLoad on awake. Yet when the scene reloads, even though it has no parent, it is still destroyed.

If the object has no parent on its Awake function where DontDestroyOnLoad is called, it is never destroyed.

If the object is a child of another object, BOTH objects have DontDestroyOnLoad, but during the scene the object receives transform.parent = null. On next scene load, the object is destroyed.

I have been hitting this error a couple of times.

The way how I implemented in order to not have troubles with using DontDestroyOnLoad() is that all the objects that I have decided to use in other scenes I had placed them in the Root of the previous scene, this way the scene loader async do not destroy their parrents and the obj keeps living trough the scene load. :slight_smile: