Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice
  4. Join us on Thursday, September 29, for a day with Unity's SRP teams here on the forum or on Reddit, and discuss topics around URP, HDRP, and the Scriptable Render Pipeline in general.
    Dismiss Notice

how to deactivate all cameras in a list except for the one that is currently in use?

Discussion in 'Scripting' started by Sexypizza92, Sep 23, 2022 at 12:33 PM.

  1. Sexypizza92

    Sexypizza92

    Joined:
    Oct 1, 2020
    Posts:
    16
    I have a list of cameras that the player can switch through using the up and down keys to look around with.


    Instead of all cameras being on at the same time when the game starts, I would like it if they were all off until the player switches over to them, then it becomes active. then if I switch again the next camera becomes active and the previous becomes inactive and so on and so forth.

    The point is to have only one camera active at a time in the game scene and that camera is the camera in current use by the player.

    How do I do this?

    Code (CSharp):
    1. public class CameraManager : MonoBehaviour
    2. {
    3.    
    4.     public List<Camera> cameras = new List<Camera>();
    5.    
    6.     public int currentCamera;
    7.  
    8.     public void Awake ()
    9.     {
    10.         cameras.Add(Camera.main);
    11.         cameras.AddRange(FindObjectsOfType<Camera>());
    12.     }
    13.  
    14.     void incCamera()
    15.     {
    16.         cameras [currentCamera].enabled = false;
    17.  
    18.         currentCamera++;
    19.  
    20.         if(currentCamera >= cameras.Count){
    21.             currentCamera = 0;
    22.         }
    23.  
    24.         cameras [currentCamera].enabled = true;
    25.     }
    26.  
    27.     void decCamera()
    28.     {
    29.         cameras [currentCamera].enabled = false;
    30.         currentCamera--;
    31.  
    32.         if(currentCamera < 0){
    33.  
    34.             currentCamera = cameras.Count-1;
    35.         }
    36.         cameras [currentCamera].enabled = true;
    37.     }
    38.  
    39.     public void Update()
    40.     {
    41.         if (Input.GetKeyUp(KeyCode.UpArrow))
    42.         {
    43.             incCamera();
    44.         }
    45.         if (Input.GetKeyUp(KeyCode.DownArrow))
    46.         {
    47.             decCamera();
    48.         }
     
  2. me2539901lreplco

    me2539901lreplco

    Joined:
    Nov 11, 2021
    Posts:
    33
  3. Sexypizza92

    Sexypizza92

    Joined:
    Oct 1, 2020
    Posts:
    16

    yeah I tried to use SetActive but I got this error


    "message": "'Camera' does not contain a definition for 'SetActive' and no accessible extension method 'SetActive' accepting a first argument of type 'Camera' could be found (are you missing a using directive or an assembly reference?


    I guess because the list I have is for Cameras and those don't count as game objects?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    7,864
    SetActive is a GameObject thing. You could try reading the documentation link provided above. ;)

    What's wrong with enabling/disabling them as you're doing? Are you saying your code doesn't work somehow? It would be worth saying that if it's the case.

    Code (CSharp):
    1. cameras.Add(Camera.main);
    2. cameras.AddRange(FindObjectsOfType<Camera>())
    You'll be adding the camera marked as main twice here.
     
  5. me2539901lreplco

    me2539901lreplco

    Joined:
    Nov 11, 2021
    Posts:
    33
    Oh yeah, that was incorrect for me. I forgot that for my game I stored the camera as a GameObject and not a Camera. Knowing that, your code should work.
     
    Sexypizza92 likes this.
  6. Sexypizza92

    Sexypizza92

    Joined:
    Oct 1, 2020
    Posts:
    16

    at the moment the code only allows me to switch through the cameras in the list but it does not deactivate/activate any cameras, that is the part that isn't working. All cameras are always on. Sorry if that wasn't clear.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    7,864
    What does it do? Have you looked to see if the camera components are being disabled? Have you checked the code above is running when you release those keys? In short, what is happening?

    Are all the cameras initially disabled manually by you? I only ask because the script above doesn't do that.

    Try getting all the cameras initially using Camera.allCameras then iterate them and disable them all apart from the first one.

    Roughly like this:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CameraManager : MonoBehaviour
    4. {
    5.     public Camera[] allCameras;
    6.      
    7.     public int currentCamera;
    8.    
    9.     public void Awake ()
    10.     {
    11.         allCameras = Camera.allCameras;
    12.  
    13.         for(var i = 1; i < allCameras.Length; ++i)
    14.             allCameras[i].enabled = false;
    15.     }
    16.    
    17.     void incCamera()
    18.     {
    19.         allCameras[currentCamera].enabled = false;
    20.         currentCamera = ++currentCamera % allCameras.Length;
    21.         allCameras[currentCamera].enabled = true;
    22.     }
    23.    
    24.     void decCamera()
    25.     {
    26.         allCameras[currentCamera].enabled = false;
    27.         currentCamera = --currentCamera < 0 ? allCameras.Length-1 : currentCamera;
    28.         allCameras[currentCamera].enabled = true;
    29.     }
    30.    
    31.     public void Update()
    32.     {
    33.         if (Input.GetKeyUp(KeyCode.UpArrow))
    34.         {
    35.             incCamera();
    36.         }
    37.  
    38.         if (Input.GetKeyUp(KeyCode.DownArrow))
    39.         {
    40.             decCamera();
    41.         }
    42.     }
    43. }
    44.  
     
unityunity