Jump to content
  • Advertisement
Sign in to follow this  
LiziPizi

Having the same camera in all scenes

This topic is 632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

You need to add a delegate to the SceneManager to tell you when scenes have loaded. You can then run code which removes the excess cameras.
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html
Something like this:
  

using UnityEngine.SceneManagement;

// Put this in a monobehavior on your main camera
void OnEnable() {
    SceneManager.sceneLoaded += OnSceneLoaded;
}

// Put this in a monobehavior on your main camera
void OnDisable() {
    SceneManager.sceneLoaded -= OnSceneLoaded;
}

// Untested, but you get the idea
private void OnSceneLoaded(Scene scene, LoadSceneMode mode) {
    // Remove all cameras except this one
    foreach (Camera c in Camera.allCameras) {
    if (c.gameObject != this.gameObject) {
         Destroy(c);
    }
}

Share this post


Link to post
Share on other sites

Are the other camera's there for testing? 

You could give your scene one camera a tag, then write a script for the other camera's that destroy's themself on start if they find a object with that tag, but tbh Kylotans answer is way more eloquent. :rolleyes:  

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!