• 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 /
This question was closed Jan 29, 2014 at 06:21 AM by AlucardJay for the following reason:

Other : Asking for Scripts

avatar image
0
Question by mitos · Jan 29, 2014 at 05:59 AM · cameraobject

How can i switch between cameras with just clicking a cube, a sphere, any object?

i have 2 cameras and a cube, i want to switch between cameras with a click on the cube

Thank you

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by supernat · Jan 29, 2014 at 06:14 AM

Add a new script to the cube with an override of the OnMouseDown() method like follows:

 public Camera cameraA;
 public Camera cameraB;
 
 void OnMouseDown()
 {
   cameraA.enabled = !cameraA.enabled;
   cameraB.enabled = !cameraB.enabled;
 }

T$$anonymous$$s assumes you enable only one camera in the editor before starting and that you have a reference to cameraA and cameraB. You just drag them onto the cube in the editor.

Also, t$$anonymous$$s will work on mobile devices as they receive the OnMouseDown event with a finger down.

Let's say you want to add t$$anonymous$$s behavior to 20 cubes, you don't want to assign the cameras to every cube, and the cameras don't exist if you make the cube a prefab, so you could instead attach a more complicated script like t$$anonymous$$s to the cube and it can become a prefab:

 static Cameras[] _cameras = null;
 
 void Start()
 {
   if (_cameras == null)
     _cameras = GameObject.FindObjectsOfType<Camera>();
 }
 
 void OnMouseDown()
 {
   foreach (Camera cam in _cameras)
     cam.enabled = !cam.enabled;
 }

Now you can support 2 cameras and unlimited cubes. Just make sure the "Camera component of only one actual camera is enabled at startup and all others are disabled. Don't disable the game object, or it won't be found with FindObjectsOfType() above. Just disable the "Camera" component.

If you want to go a step further and support more than 2 cameras, you could do the following to loop through them all:

 static Cameras[] _cameras = null;
 static int _activeCamera = 0;

 void Start()
 {
   if (_cameras == null)
     _cameras = GameObject.FindObjectsOfType<Camera>();
 }

 void OnMouseDown()
 {
   _cameras[_activeCamera].enabled = false;
   _cameras[(_activeCamera = _activeCamera + 1) % _cameras.Length].enabled = true;
 }

Well you get the idea.

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 mitos · Jan 29, 2014 at 06:28 AM 0
Share

tyvm, upvote =D

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to set the position of objects on the left and right of the screen 0 Answers

Object doesn't stick on Camera right Corner?? (Basic Question) 1 Answer

How to Detect a portion of object in Camera or not ? 1 Answer

Typecasting Camera to Object for ObjectField 3 Answers

How can I change camera when colliding with "X" object? 1 Answer


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