2
\$\begingroup\$

I have 100 scenes, each scene has a camera, but I want only the camera from scene 1 to exist in each scene that I appear in.

I use the function DontDestroyOnLoad() to make the camera exist in each scene but I always find my self with another camera that I don't want.

Any ideas? thank you in advance.

\$\endgroup\$
  • \$\begingroup\$ Any one? I need help! \$\endgroup\$ – LiziPizi Mar 13 '17 at 10:48
2
\$\begingroup\$

You need to make sure you remove the camera in every scene except the first one. Assuming you HAVE to keep the first camera for some reason, you probably then need to re position the camera somewhere in your subsequent scenes.

\$\endgroup\$
1
\$\begingroup\$

You can use singleton pattern or add some simple functionality destroying newly created camera if it's already present in scene.

\$\endgroup\$
1
\$\begingroup\$

Another option is to have one "master scene" with the camera (and anything else you want to have in every level, like UI elements for example) and then load the individual levels additively with SceneManager.LoadSceneAsync(scene, LoadSceneMode.Additive). You will then have two scenes at once loaded in your game. If you want to switch levels, you can unload the current level-scene with SceneManager.UnloadSceneAsync(scene).

Remember to wait until UnloadSceneAsync has finished before calling LoadSceneAsync. Otherwise you might occasionally see objects from the new scene interact with objects from the old which were not deleted yet. You can do that by examining the AsyncOperation object returned by LoadSceneAsync or by assigning a callback method to SceneManager.sceneUnloaded.

\$\endgroup\$
1
\$\begingroup\$

It depends on what you want to achieve:

You want to preserve the camera through all the levels in you scene

Then what you want to use is a script with Object.dontDestroyOnLoad()

You want to use a camera in all your levels, but don't want to preserve the object throughout all the scenes

This is a work for prefabs. You make a prefab of you camera and apply components, scripts and so on to it. You have to remember to have a "camera" prefab object in every scene. If you want to make changes to it, you just have to modify the prefab, not all the "camera" objects in the scenes

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.