Unity DontDestroyOnLoad() doesnt work

When I tried to make a empty GameObject go to another scene with DontDestroyOnLoad(), it doesnt worked.
It shows up under the DontDestroyOnLoad in the hierarchy, but if I change the scene it isnt there anymore. Maybe Im using it wrong, because Im new to Unity, but I think yoou guys can help me :slight_smile:

Here my Code:

using UnityEngine;
using System.Collections;

public class dontdestroyonload : MonoBehaviour{

    void Awake(){
        DontDestroyOnLoad(gameObject);
    }

}

It has to be a root gameobject. Are you seeing an yellow warning?

Instead of the above, always do:

DontDestroyOnLoad(transform.root.gameObject);  // ensure my entire hierarchy is DDOL

Thank you so much! Im glad that it works now!!!

1 Like

Just so you know, this is a general scripting problem, not a 2D question. I would recommend posting scripting questions in the dedicated forum here.