Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by latsushi · Apr 13, 2012 at 03:55 PM · c#cameraswitchswap

Switch Camera on Input in C#

pragma

using UnityEngine; using System.Collections;

public class MenuPrototypes : MonoBehaviour {

 bool cam = true;
 bool cam2 = false;
 
 void Start ()
 {
     bool cam = true;
     bool cam2 = false;

I need help with switching from one camera to another by pressing 'Enter.' Here is my code. All the examples I see are in JavaScript but not C# : /. Any help is truly appreciated. I have been trying to figure this out for three days.

Please be as specific as possible as I am not good at scripting.... obviously. }

 void SwapCamera ()
 {
     if (Input.GetKeyUp(KeyCode.Return))    {            
     bool cam = false;
     bool cam2 = true; 
 }

     }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image latsushi · Apr 13, 2012 at 02:27 PM 0
Share

Apparently I put by comments right in the middle of my code. I didn't mean to do that. : /

4 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

Answer by centaurianmudpig · Apr 13, 2012 at 05:24 PM

You are declaring the cam and cam2 variables locally, by using the bool type each time. Once you declare a variable, like you did at the top, you reference it without the type, i.e. cam = true;

I'll assume you have not referenced any camera's. Add these two lines, replacing cam and cam2 variables at the top:

 public Camera camera;
 public Camera camera2;

Then change your start routine:

 void Start() {
 camera.enabled = true;
 camera2.enabled = false;
 }

 void Update() {
 //This will toggle the enabled state of the two cameras between true and false each time
 if (Input.GetKeyUp(KeyCode.Return)) {
 camera.enabled = !camera.enabled;
 camera2.enabled = !camera2.enabled;
 }

Add your code to a GameObject, NOT the camera's. Attach your camera's to Camera and Camera2 against this GameObject in the inspector panel. Run the code and it should switch between the 2 camera's. Setup a scene so you can see it switch. This hasn't been tested, I wrote it straight in here but it should work.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Giantbean · Sep 20, 2013 at 04:37 PM 0
Share

I am having some issues with switching the camera more than once. I set one camera to load I then click a gameObject and the camera changes. This camera has a GUI object that can be clicked to switch back to the first camera. So far so good. However if I click the gameObject again I cant go back into the second Camera.

I am using Culling layers and tags to show and hide objects but even without those masked layers I can only get the camera switches to work once.

Any ideas how to make this work where I can switch back and forth as many times as my heart desires?

Also thank you for the above test code. It was a great starting point.

avatar image vkishan96 · Jul 03, 2019 at 07:43 AM 0
Share

cam u post the full code

avatar image chrisvanrens · Jun 22, 2021 at 11:11 AM 0
Share

Thank you. This worked perfectly for what I needed. As you can see, I used GetKeyDown instead.

Here's my code:

 public class CameraSwitch : MonoBehaviour
 {
     public Camera Camera1;
     public Camera Camera2;
 
     // Start is called before the first frame update
     void Start()
     {
         Camera1.enabled = true;
         Camera2.enabled = false;
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.C))
         {
             Camera1.enabled = !Camera1.enabled;
             Camera2.enabled = !Camera2.enabled;
         }
 
     }
avatar image
0

Answer by XtriMiles · Jul 04, 2014 at 03:32 AM

This works but I can see my body in the second camera, it looks like I'm teleporting

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by tdneren2 · Aug 24, 2014 at 06:04 PM

For any developer who reads this - camera change does not work after exporting to an exe-file

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tdneren2 · Aug 24, 2014 at 06:06 PM 0
Share
  • nor works after exporting to webplayer

avatar image
0

Answer by FuturescoGames · May 05, 2017 at 01:19 PM

Thanks a lot, good idea, but I have some problems with this so I change it to use SetActive and change it to use any number of cameras or any kind objects:

 using UnityEngine;
 
 public class ChangeObject : MonoBehaviour
 {
 
     public GameObject[] objects;
     private int objectActual = 0;
 
     void Start()
     {
         DisableObjects();
         objects[0].gameObject.SetActive(true);
     }
 
     void Update()
     {
         if (Input.GetKeyUp(KeyCode.Return))
         {
             DisableObjects();
             objectActual++;
             if (objectActual >= objects.Length)
             {
                 objectActual = 0;
             }
             objects[objectActual].gameObject.SetActive(true);
         }
     }
 
     void DisableObjects()
     {
         foreach (GameObject obj in objects)
         {
             obj.gameObject.SetActive(false);
         }
     }
 }
 
 
 

I hope it could be usefull

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

switching cameras C# 4 Answers

Camera switching : C# onTriggerEnter not working 1 Answer

Multiple Cars not working 1 Answer

Swapping Cameras Disables Keyboard Input 3 Answers

Distribute terrain in zones 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges