Unity: Editing a DontDestroyOnLoad object in another scene

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?

Solutions/Answers:

Answer 1:

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)

Answer 2:

Try using the new SceneManagerAdditiveLoading function. You can view a tutorial at YouTube.

Answer 3:

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.

Answer 4:

Try this:

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

You will get an object reference in each scene.

References