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?
public class CameraManager : MonoBehaviour
{
public List<Camera> cameras = new List<Camera>();
public int currentCamera;
public void Awake ()
{
cameras.Add(Camera.main);
cameras.AddRange(FindObjectsOfType<Camera>());
}
void incCamera()
{
cameras [currentCamera].enabled = false;
currentCamera++;
if(currentCamera >= cameras.Count){
currentCamera = 0;
}
cameras [currentCamera].enabled = true;
}
void decCamera()
{
cameras [currentCamera].enabled = false;
currentCamera--;
if(currentCamera < 0){
currentCamera = cameras.Count-1;
}
cameras [currentCamera].enabled = true;
}
public void Update()
{
if (Input.GetKeyUp(KeyCode.UpArrow))
{
incCamera();
}
if (Input.GetKeyUp(KeyCode.DownArrow))
{
decCamera();
}
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?
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.
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.
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.
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:
using UnityEngine;
public class CameraManager : MonoBehaviour
{
public Camera[] allCameras;
public int currentCamera;
public void Awake ()
{
allCameras = Camera.allCameras;
for(var i = 1; i < allCameras.Length; ++i)
allCameras[i].enabled = false;
}
void incCamera()
{
allCameras[currentCamera].enabled = false;
currentCamera = ++currentCamera % allCameras.Length;
allCameras[currentCamera].enabled = true;
}
void decCamera()
{
allCameras[currentCamera].enabled = false;
currentCamera = --currentCamera < 0 ? allCameras.Length-1 : currentCamera;
allCameras[currentCamera].enabled = true;
}
public void Update()
{
if (Input.GetKeyUp(KeyCode.UpArrow))
{
incCamera();
}
if (Input.GetKeyUp(KeyCode.DownArrow))
{
decCamera();
}
}
}