- Home /
Switching Between Cameras
Hey guys, I'm still struggling with trying to switch between two cameras. I found a few examples on here but havent been able to quite figure it out yet.
whats the easiest way to do this? if i have say:
Camera1 Camera2
Im trying to use this example right now:
I made a script with just that code in it. he says to just call it with "SelectCamera(1);" I just cant figure out the syntax. so if i want to call camera2 from a diffrent script what would it be?
thanks
Answer by NoName115 · May 08, 2014 at 09:55 AM
Try this: If you want to look at Camera2:
Camera1.enabled = false; Camera2.enabled = true;
for Camera1 same code but change false/true.
ok i tried this. and the problem is that im getting 'The variable Camera1 of 'LoginScreen'(name of my script) has not been assigned.
Well i cant drag the cameras onto the script because its looking for a game object and it wont let me drag the cameras in the inspector. :(
Im doing this:
public GameObject Camera1;
public GameObject Camera2;
void Start(){
Camera1.camera.active = true;
Camera2.camera.active = false;
}
then i get that error on start. and like i said it wont let me drag the cameras in the inspector on the script.
Try declaring the variable as "Camera" ... ie.
Camera Camera1;
Camera Camera2;
did that, still wont let me drag cameras in.
"the variable Camera1 of 'LoginScreen' has not been assigned. :/
and when i click the little circle to the right of where you drag the camera in the inspector it doesn't show any cameras in the list to pick from. when in fact I have two..
ok i figured out how i want to do this i think.. i am using a separate script again that i will call whenever i want to switch cameras..
public class CameraSwitch : $$anonymous$$onoBehaviour {
public GameObject Camera1;
public GameObject Camera2;
// Use this for initialization
void Start () {
}
// Update is called once per frame
public void ChangeCamera () {
if (Camera1.camera.active == true)
{
Camera2.camera.active = true;
Camera1.camera.active = false;
}
if (Camera2.camera.active == true)
{
Camera1.camera.active = true;
Camera2.camera.active = false;
}
}
}
i will just call this whenever i want to switch how do i call this from another script though?
is it like: CameraSwitch.ChangeCamera();
^ ok i did that and get an error: an object refernce is required to access non-static member 'cameraswitch.changecamera()
after doing some reading looks like i need to create an instance? meh not sure how that works yet.. (looking into it)
I changed your code, only little:
public class CameraSwitch : $$anonymous$$onoBehaviour {
public GameObject Camera1;
public GameObject Camera2;
public void ChangeCamera () {
if (Camera1.camera.active)
{
Camera2.camera.active = true;
Camera1.camera.active = false;
}
else if (Camera2.camera.active)
{
Camera1.camera.active = true;
Camera2.camera.active = false;
}
}
Or you can use this code but you have try if it will be work:
public void ChangeCamera () {
Camera1.camera.active = !Camera1.camera.active;
Camera2.camera.active = !Camera2.camera.active;
}
This code works like, if (true) set false, if (false) set true.
When you want call this script from another object you have to do write this:
GameObject.GetComponent<CameraSwitch> ().ChangeCamera();
Remember you have to attach Camera1 and Camera2 to script inside your GameObject. I hope that i helped you.
Answer by vbs · Mar 09, 2015 at 09:00 AM
This is what I use in C# Unity 5
using UnityEngine;
using System.Collections;
public class CameraSwitcher : MonoBehaviour {
[SerializeField] private GameObject[] cameras = new GameObject[5];
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("1") && cameras[0] != null)
{
switchCameras(0);
}
else if (Input.GetKeyDown("2") && cameras[1] != null)
{
switchCameras(1);
}
else if (Input.GetKeyDown("3") && cameras[2] != null)
{
switchCameras(2);
}
else if (Input.GetKeyDown("4") && cameras[3] != null)
{
switchCameras(3);
}
else if (Input.GetKeyDown("5") && cameras[4] != null)
{
switchCameras(4);
}
}
private void switchCameras(int keyNum) {
for (int i = 0; i < cameras.Length-1; i++) {
if (cameras[i] != null && keyNum != i) {
// turn camera off
cameras[i].GetComponent<Camera>().enabled = false;
} else {
// turn camera on
cameras[i].GetComponent<Camera>().enabled = true;
}
}
}
}
I'm not sure why you have i < cameras.Length-1
in the switchCameras for loop. I had to remove the -1 to get it working
Hello How can I attach this to my scene. Do I need to create 4 cameras or attach this code to all cameras
You just need to attach this script to the gameobject you want, not necessary the cameras, it can be a simple empty. Then, you have to drag & drop them the desired cameras in the inspector.
Answer by Ouija · Mar 08, 2015 at 08:57 PM
I got an answer to this problem last week, using UI buttons, works perfectly. Maybe it will help. http://answers.unity3d.com/questions/911949/ui-buttons-switch-cameras.html