Search Unity

Scene Load Duplicates Main Camera??

Discussion in '2D' started by CoroKoro, Mar 5, 2016.

  1. CoroKoro

    CoroKoro

    Joined:
    Mar 24, 2015
    Posts:
    21
    Hi guys!
    I'm having a bit of an issue with my scene load and reload for my multilevel game. Every time I beat a level in my game and change the scene, a duplicate of my Main camera is made. This messes with all of my screentoworld stuff with my mouseposition and aiming of my player. How can I make sure that a second main camera isn't created when I load a scene?
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    cameras cannot create them themself. Do you use DontDestroyOnLoad ? Or something in new level add new camera.
     
    Last edited: Mar 6, 2016
  3. rslnautic

    rslnautic

    Joined:
    Nov 16, 2015
    Posts:
    5
    Use singleton construction pattern, inside your camera script to have only one instance of your camera.
    It will fix your problem of duplication.
    If you have other duplication problem like player or gamemanager use singleton! :)

    Code (CSharp):
    1. public class Camera2D : MonoBehaviour {
    2.     //Singleton
    3.     private static Camera2D _instance;
    4.     public static Camera2D Instance { get { return _instance; } }
    5.  
    6.     private void Awake()
    7.     {
    8.         //Singleton
    9.         if (_instance != null && _instance != this)
    10.         {
    11.             Destroy(this.gameObject);
    12.         } else {
    13.             _instance = this;
    14.         }
    15.     }
    16. }
     
  4. quizcanners

    quizcanners

    Joined:
    Feb 6, 2015
    Posts:
    109
    I am very aware of singletons, but only recently started working with multi-scene setup, had "Multiple audio listeners" message spamming the console every frame and realizing that I'm not doing anything special in terms of the workflow, so I started looking around for what is the correct way to handle this.

    Apparently, making a custom singleton is the way. Which makes sense. I've added mine below if anyone is interested. Could be reused for many objects by expanding the enum.

    Code (CSharp):
    1.   public class GameObjectSingleton : MonoBehaviour
    2.     {
    3.         [SerializeField] private SingletonRole _role;
    4.  
    5.         private enum SingletonRole
    6.         {
    7.             MainCamera = 0,
    8.             Canvas = 1,
    9.             Player = 2
    10.         }
    11.  
    12.         private static Dictionary<SingletonRole, GameObject> _inTheScene = new Dictionary<SingletonRole, GameObject>();
    13.  
    14.         private void Awake()
    15.         {
    16.             if (_inTheScene.TryGetValue(_role, out GameObject go) && go && go != gameObject)
    17.             {
    18.                 Destroy(gameObject);
    19.             } else
    20.             {
    21.                 _inTheScene[_role] = gameObject;
    22.             }
    23.         }
    24.     }
    25.