Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity C script (Multi_Cameras)

Discussion in 'Scripting' started by Fuzzylr, May 11, 2013.

  1. Fuzzylr

    Fuzzylr

    Joined:
    May 8, 2013
    Posts:
    18
    I have done some Googling. I found a lot of scripts referring to Java but I have yet to find one for C. What I want to do is be able to assign two camera's like they did in the Java script but using C. I am semi new to coding in C so I need a bit of help calling back so you can set the camera's inside the GUI. I already have the if statement wrote out for being able to swap the cameras. I haven't figure out how to assign the camera using C. How can I assign the cameras? I would be just as happy setting them in the script or drag and dropping in them in the GUI. Either or is fine.

    Sean
     
  2. xtremepman

    xtremepman

    Joined:
    Jul 18, 2012
    Posts:
    388
    I assume you mean C#. I did a quick search and converted come Unityscript into c# for you. You could use an array if you have too many cameras:D

    Code (csharp):
    1.  
    2.  
    3. Camera camera1;
    4. Camera camera2;
    5.  
    6. void Update (){
    7.  
    8. if (Input.GetKeyDown("c")){
    9.  
    10. if (camera1.enabled == true){
    11.  
    12. camera1.enabled = false;
    13. camera2.enabled = true;
    14.  
    15. }
    16.  
    17. else
    18.  
    19. {
    20.  
    21. camera1.enabled = true;
    22. camera2.enabled = false;
    23.  
    24. }
    25.  
    26. }
    27.  
    28. }
    29.  
    30. }
    31.  
    32.  
     
  3. Fuzzylr

    Fuzzylr

    Joined:
    May 8, 2013
    Posts:
    18
    I do have a small problem. I keeps complaining about camera 2 being null. It's not assigned.
     
  4. Fuzzylr

    Fuzzylr

    Joined:
    May 8, 2013
    Posts:
    18
    Ok, I fixed the first problem. I just simply did the following.

    Code (csharp):
    1. public class Cameras : MonoBehaviour
    2. {  
    3.     public Camera Camera1;
    4.     public Camera Camera2;
    However, it's not switching cameras now. Here is my code for this.
    Code (csharp):
    1.  
    2. // Use this for initialization
    3.     void Start()
    4.     {
    5.         Camera1.enabled = true;
    6.         Camera2.enabled = false;
    7.     }
    8.    
    9.     // Update is called once per frame
    10.     void Update()
    11.     {
    12.         // Switch camera views
    13.         if( Input.GetKeyDown( KeyCode.Tab ) ) // First to 3rd
    14.         {
    15.             Camera1.enabled = false;
    16.             Camera2.enabled = true;
    17.         }
    18.         else //Switch back
    19.         {
    20.             Camera1.enabled = true;
    21.             Camera2.enabled = false;
    22.         }  
    23.     }
    24. }
    25.