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. Dismiss Notice

[SOLVED]Switch Camera

Discussion in 'Scripting' started by digitalwm, Sep 28, 2009.

  1. digitalwm

    digitalwm

    Joined:
    Sep 21, 2009
    Posts:
    12
    Hi everybody,

    How can I switch the player view from one camera to another. I have tried the following way:

    Code (csharp):
    1.  
    2. GameObject.Find("Initial Camera").GetComponent("Camera").active = false;
    3. GameObject.Find("Main Camera").GetComponent("Camera").active = true;
    4.  
    and is not working. Any ideas will be welcomed.

    Thanks.
     
  2. ChrisBennet

    ChrisBennet

    Joined:
    Feb 15, 2009
    Posts:
    9
    digitalwm,
    I'm not sure why yours isn't working. Here is what I am using:
    Code (csharp):
    1.    
    2.     GameObject _cameraFP = null;
    3.     GameObject _cameraWV = null;
    4.    
    5.     void Start ()
    6.     {
    7.         // Init camera
    8.         _cameraFP = GameObject.Find("Main Camera");
    9.         if (_cameraFP == null)
    10.             Debug.Log("Start(): First Person Camera not found");
    11.            
    12.         _cameraWV = GameObject.Find("Main Camera Wide View");
    13.         if (_cameraWV == null)
    14.             Debug.Log("Start(): Wide View Camera not found");
    15.     }
    16.    
    17.     void SelectCamera(int cameraIndex)
    18.     {
    19.         if (_cameraFP != null)
    20.             _cameraFP.camera.enabled = (cameraIndex == 0);
    21.         if (_cameraWV != null)
    22.             _cameraWV.camera.enabled = (cameraIndex == 1);
    23.    
    24.     }
    25.  
    Hope that helps,
    -Chris
     
  3. digitalwm

    digitalwm

    Joined:
    Sep 21, 2009
    Posts:
    12
    Thanks - Solved.
     
  4. trinitysj

    trinitysj

    Joined:
    Jan 15, 2010
    Posts:
    54
    where do you put this and how do you use it? on a camera or the main camera or the player not sure. and how to you make it switch, through a getkeydown or soemthing like that?


    thanks
     
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You can put this anywhere convenient, say on an empty object somewhere in the scene acting as a camera controller. You can use any event you like to switch cameras, so it could be a keystroke, but could just as easily be an event in the game.
     
  6. solid

    solid

    Joined:
    Jan 15, 2010
    Posts:
    132
    i tried

    Code (csharp):
    1.  
    2. GameObject.Find("InitialCamera").GetComponent("Camera").active = false;
    3. GameObject.Find("MainCamera").GetComponent("Camera").active = true;
    4.  

    and it worked for me. dont know what you could have done different then me.
     
  7. Disati

    Disati

    Joined:
    Oct 5, 2009
    Posts:
    111
    Tell us what you did!
     
  8. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    probably he has a third (or first) person controller where in somewhere is calling the Camera.main.transform... i guess..
     
  9. Rubik

    Rubik

    Joined:
    Nov 19, 2012
    Posts:
    1
    Thanks!