# Unity: Editing a DontDestroyOnLoad object in another scene

I figured out how to make certain objects like my Camera and Canvas persist across scenes, but I can't figure out how to access them in the editor from scenes in which they're not instantiated.

I can make these objects prefabs and make my changes to the prefab rather than the instance, but then I can't make references in the prefab to objects in the hierarchy; I would have to use an instance of a prefab to do that.

How can I edit subsequent scenes in the same way that I edited the first scene, where all the DontDestroyOnLoad objects are instantiated for the first time and I can access them through the hierarchy?

I figured out how to make certain objects like my Camera and Canvas persist across scenes, but I can't figure out how to access them in the editor from scenes in which they're not instantiated.

A scene is basically a collection of objects (and serialized properties) that the engine bundles and can load together. As such, it's allowed to cross reference objects that belong to the same scene.

DontDestroyOnLoad mark an object in such a way that when a non additive scene loading operation(Apllication.LoadLevel|LoadLevelAsync) is performed, that object won't be destroyed. It controls the runtime lifetime of an object. In editor mode, isn't possible to reference an object across different scenes.

I can make these objects prefabs and make my changes to the prefab rather than the instance, but then I can't make references in the prefab to objects in the hierarchy; I would have to use an instance of a prefab to do that.

That's how it works, right. If you want to setup references in one of your persistent object you basically have 2 ways:

1. Instantiate (or save it into a scene) once, prevent it gets destroyed(DontDestroyOnLoad), and initialize reference at runtime when a different level is loaded (es. OnLevelWasLoaded)
2. Save a prefab instance of such objects in each scene, and serialize references you need into the relative instances.

How can I edit subsequent scenes in the same way that I edited the first scene, where all the DontDestroyOnLoad objects are instantiated for the first time and I can access them through the hierarchy?

If you are asking how to reference in a Scene_B a prefab is supposed to be instantiated and not destroyed in a Scene_A, in editor mode then you can't. The object instance is only available when a particular scene will be loaded (at runtime or in the editor for editing)

• There may be a method, using custom editor scripts, where a specified set of objects are spawned when a scene is loaded in editor, and any changes to them saved out to the corresponding prefabs. I don't have a pre-made solution of this type to point to just yet though. You're right that, without extending the editor functionality, this is not available out of the box. – DMGregory Apr 22 '15 at 19:58
• There are way to detect if a scene is changed/loaded in the editor, but still not possible in anyway cross reference objects across different scenes. – Heisenbug Apr 22 '15 at 20:07

• I think using additive scene loading is a good direction for solving this problem. Fleshing this out to a complete example (without relying on an external video) would make this answer even better. – DMGregory Oct 7 '16 at 22:32

So you have:

• Scene one that contains the camera and canvas (which have been marked don't destroy on load). It also has a script that does a load for scene two.
• Scene two that contains objects that need to reference the canvas in scene one.

There's a few solutions for this:

Solution one (low effort)

Use GameObject.Find in Scene two to find the relevant objects in scene one, and assign them to your scripts.

Solution two

Create a singleton manager, which will avoid the sometimes expensive GameObject.Find call. A script in scene two can reference variables mediated by the singleton.

Try this:

var GameObject object=GameObject.Find("object name that won't be destroyed");


You will get an object reference in each scene.

• This will work once the object has been instantiated, but it will not work straight from the editor in an alternate scene, as this question asks. – Gnemlock Aug 20 '16 at 23:44