Simple Camera Switch Using C#
Hello, I'm new to the land of Unity and I'm trying to muddle my way through. I'm probably having issues due to inexperience using Unity and C#.
In very simple scene, I'm trying to make 2 camera's switch when a key is pressed on the board (C). Is there a simple method to achieve this using enabled = true?
Other notes that could be of use:
I have followed other vid tuts without success. :-( I have nested 2 camera's in a a Empty Game Object... do I need to do this?
I'm a newb.
Thanks for you help.
The End Script - -
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CameraSwitch : $$anonymous$$onoBehaviour {
 public Camera camera1;
 public Camera camera2;
 private void Awake ()
 {
     camera2.enabled = false;
 }
 public void SwitchCam ()
 {
     camera1.enabled = !camera1.enabled;
     camera2.enabled = !camera2.enabled;
 }
     
 void Update()
 {
     if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.C))
     {
         Debug.Log ("C key was pressed");
         SwitchCam ();
     }
 }
}
Answer by UnityCoach · Feb 26, 2017 at 01:08 PM
You can group your cameras, but don't have to.
Regarding the switch, you want to make sure you activate one and not the other. Something in the like :
 public Camera camOne;
 public Camera camTwo;
 
 private void Awake ()
 {
     camTwo.enabled = false;
 }
 
 public void SwitchCam ()
 {
     camOne.enabled = !camOne.enabled;
     camTwo.enabled = !camTwo.enabled;
 }
This is one of many ways to do it.
Thanks UnityCoach. How would I go about making this work on a keybaord press? For example if I pressed "C" ?
if (Input.Get$$anonymous$$eyDown("C"))
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CameraSwitch : $$anonymous$$onoBehaviour {
 public Camera camera1;
 public Camera camera2;
 private void Awake ()
 {
     camera2.enabled = false;
 }
 public void SwitchCam ()
 {
     camera1.enabled = !camera1.enabled;
     camera2.enabled = !camera2.enabled;
 }
}
Et voilà !
 void Update ()
 {
     if (Input.Get$$anonymous$$eyDown("C"))
         SwitchCam ();
 }
Thanks UC... although it has spat back this error...
"ArgumentException: Input $$anonymous$$ey named: C is unknown UnityEngine.Input.Get$$anonymous$$eyDown (System.String name) (at /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/InputBindings.gen.cs:425) CameraSwitch.Update () (at Assets/CameraSwitch.cs:24)"
Is there something that I have failed to add in the editor?
Thanks!
Answer by jimm84 · Feb 28, 2017 at 07:40 AM
Morning... Managed to stop it spitting back errors but the camera transitions doesn't work. Another thing to note is that the object in my scene is flash/blinking rapidly. Any clues?
using System.Collections; using System.Collections.Generic; using UnityEngine;
 public class CameraSwitch : MonoBehaviour {
 public Camera camera1;
 public Camera camera2;
 private void Awake ()
 {
     camera2.enabled = false;
 }
 public void SwitchCam ()
 {
     camera1.enabled = !camera1.enabled;
     camera2.enabled = !camera2.enabled;
 }
     
     void Update() {
     if (Input.GetKeyDown(KeyCode.C))
             print("C key was pressed");
         SwitchCam ();
     }
}
Thanks,
Ah, if you want to add more than one line of code to an if statement, you need opening and closing brackets.
 void Update()
 {
      if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.C))
     {
         Debug.Log ("C key was pressed");
         SwitchCam ();
     }
 }
Hello Unity Coach, thank you for your help! I have also tested this on my mac and it works. I might add some comments to this at some stage. Cheers Thank you!
Answer by Vampyr_Engel · Apr 15, 2018 at 05:12 AM
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Manycameras : MonoBehaviour {
 
     public Camera camera1;
     public Camera camera2;
     public Keycode key;
 
     private void Awake ()
     {
         camera2.enabled = false;
     }
 
     public void SwitchCam ()
     {
         camera1.enabled = !camera1.enabled;
         camera2.enabled = !camera2.enabled;
     }
 }
 
 void Update()
 {
     if (Input.GetKeyDown (KeyCode.C))
     {
         Debug.Log ("C key was pressed");
         SwitchCam ();
     }
 }
Well I tried this code as I wish for more than one camera in my game I want lots as I want to be able to see from both friendlies and enemies but all I keep getting when I run this code is this
: Error CS0116: A namespace cannot directly contain members such as fields or methods (CS0116) (Assembly-CSharp)Blockquote
Your answer
 
 
             Follow this Question
Related Questions
Top Down Camera View Problem 1 Answer
What this error? 0 Answers
"Lookat" only working in one case 0 Answers
My boolean value won't get called from other script 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                