Advertisement

Having the same camera in all scenes

Started by March 12, 2017 12:19 PM
3 comments, last by Josh Green 7 years, 9 months ago

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.

Any one?

Advertisement

Nevermind -- I cannot read today.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

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);
    }
}

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:

Learn to create games through our vibrant platform of easy-to-follow video training material. Getting recognition of the knowledge and skills you gain from the courses is essential. We can provide you with globally recognised City & Guilds accredited certificates.

www.polygoncollege.com

This topic is closed to new replies.

Advertisement