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
0
Question by sonic lover · May 11, 2013 at 06:13 PM · cameraswitchcameras

cameras switch doesn't work

Hi Everyone! I'm trying to switch between cameras in unity for my game, but not a usual switch - I want them to get switched when the player is on one terrain or another (thats not the problem, I used the function

void OnTriggerEnter(Collider Other)

and used tags; the problem was to switch the cameras. I tryed to to the next code in this function^^ but it didn't work:

             GameObject.Find("Main Camera").GetComponent("Camera").camera.enabled = false;
             GameObject.Find("2D Camera").GetComponent("Camera").camera.enabled = true;

then I tryed something else - I used another code -

 public Camera[] cams;

and switched the cameras when something heppend, but this time the problem was to put the cameras in the player, and I don't understand what is wrong. can someone help me please?

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

· Add your reply
  • Sort: 
avatar image
0

Answer by Brahim113 · May 11, 2013 at 07:22 PM

Your logic worked for me. I made a fast script in 2min public class PlayWithCameras : MonoBehaviour {

     Camera _camera1;
     Camera _camera2;
 
     void Start () {
         _camera1 = GameObject.FindGameObjectWithTag("Camera1").GetComponent<Camera>();
         _camera2 = GameObject.FindGameObjectWithTag("Camera2").GetComponent<Camera>();
     }
     
     void Update () {
         if(Input.GetKeyDown(KeyCode.T))
         {
             _camera1.enabled = false;
             _camera2.enabled = true;            
         }
         if(Input.GetKeyDown(KeyCode.R))
         {
             _camera1.enabled = true;
             _camera2.enabled = false;            
         }    
     }
 }

You can improve this. But it works. You could easily do it with a array. But this is how my hierachy looks like ingame

TempCameras (Just a gameObject that has the script) I then created 2 empty gameObjects and put one camrea in each. I tagged them with Camera1 and Camera2. I put these 2 gameobjects as childobjects of the TempCameras object (But you dont have to)

Thats it. Hope this helpes you on your way.

Comment
Add comment · Show 5 · 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 sonic lover · May 12, 2013 at 05:03 AM 0
Share

Thanks, You are awesome! its not what I want, even not close actually, because I made something like this - but you fixed one of my codes! my code:

   GameObject.Find("$$anonymous$$ain Camera").GetComponent("Camera")...


your code: _camera1 = GameObject.FindGameObjectWithTag("Camera1").GetComponent();

so thank you very much! (but, if someone understood me and know what I want, can you help me too, so it will be easyer to me in the game?)

avatar image Julien-Lynge · May 12, 2013 at 05:04 AM 0
Share

Converted answer to comment.

avatar image sonic lover · May 12, 2013 at 05:04 PM -1
Share

I'm sorry, I thought it would be good, but the code didn't work for me. can someone else help me?

avatar image Julien-Lynge · May 12, 2013 at 05:36 PM 0
Share

Converted answer to comment again. Please use the comment button for comments, and the answer button for answers, or you will be downvoted and lose karma.

avatar image Brahim113 · May 12, 2013 at 05:58 PM 0
Share

I have tested my code and it works fine. If you could be more precise in what you are trying to achive. $$anonymous$$aybe i could help you out.

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

15 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

Related Questions

Multiple Camera Switching 1 Answer

character switching on collision 1 Answer

3rd Person to First Person Camera 0 Answers

Camera switch after specific time 1 Answer

How can i track/update the lastPosition of a camera when switching between cameras ? 0 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