Singleton , DontDestroyOnLoad() and scene reloading

I am having trouble with DontDestroyOnLoad() and scene reloading.
I have this script :

public class SingleInstance : MonoBehaviour
{
    private static Dictionary<string, GameObject> instances = new Dictionary<string,GameObject>();

    void Awake()
    {
        GameObject inst = null;
        instances.TryGetValue(this.name, out inst);
        if (inst == null)
        {
            DontDestroyOnLoad(this.gameObject);
            instances[this.name] = gameObject;
        }
        else
        {
            DestroyImmediate(gameObject);
        }
    }
}

I aplied this script to my UIManager object which is part of a simple 3 object Hierarchy :

UIManager

-ImageLoader

-Canvas

(this 3 object hierarchy is a prefab and was dragged and dropped in the scene).
The problem is that after reloading the scene both child objects (the Canvas and ImageLoader) get OnDestroyed called when reloading the scene. In the inspector they still look ok but in code where I access the canvas it says that it is Destroyed with this messge:

MissingReferenceException: The object
of type ‘Canvas’ has been destroyed
but you are still trying to access it.
Your script should either check if it
is null or you should not destroy the
object.

I don’t have any Ideea why the children get “flagged” as destroyed becaus the SingleInstance script only destroys the toBeCreated Object.

Note that Unity calls “OnDestroy” automatically (http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDestroy.html), OnDetroyed is a custom function that you’ll have to call manually.

I just tested with your code writing and OnDestroy function for the childs that prints their names, and it’s not being called after reloading the scene with:

Application.LoadLevel(Application.loadedLevel);

Now, for your issue, I guess you have some other object (lets call it “obj”) in your scene that has a reference to the Canvas GameObject. When you reload the scene Unity creates all the objects in the scene, setup the references (obj has a reference to the newly created Canvas inside the newly created UIManager) and on the Awake face destroys the newly created UIManager with the Canvas child. Now, obj has an invalid reference, it’s pointing to a Canvas that doesn’t exists, the Canvas that wasn’t destoyed is a different object from the previous scene.

One solution to your problem would be to replaces those static references to childs of singletons with finds in scripts. Instead of setting the reference to the Canvas in the inspector, look up for a game object called “Canvas” in some Awake or Start function.