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.

Unity 3D Enable/Disable Cameras In Hierarchy

Discussion in 'Scripting' started by NatRobson, Aug 19, 2010.

  1. NatRobson

    NatRobson

    Joined:
    Aug 19, 2010
    Posts:
    2
    Hello i am new to Untiy 3D and coding within in Unity 3D :twisted:

    I was woundering if you could help correct this code or tell me why i am unable to get it to work. :?:

    I am wanting to switch between cameras which are located in the Hierarchy?

    Code (csharp):
    1.  
    2. function Update() {
    3.  
    4. if (Input.GetButton ("e")){
    5.     {
    6.     Human_Camera.enabled(false);
    7.     Car_Camera.enabled(true);    
    8.     }
    9.    
    10. }
    11.  
    12. if (Input.GetButton ("r")){
    13.     {
    14.     Human_Camera.enabled(true);
    15.     Car_Camera.enabled(false);    
    16.     }
    17.  
    18. }
    19.  
    Thanks for your time!
     
  2. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    Does this script component know what Human_Camera and Car_Camera are?

    Their presence in the hierarchy doesn't mean much if you haven't somehow linked them.
     
  3. NatRobson

    NatRobson

    Joined:
    Aug 19, 2010
    Posts:
    2
    So would i create a empty GameObject and put the Human_Camera and Car_Camera inside the GameObject in the Hierarchy?

    then change the script to

    Code (csharp):
    1.  
    2. var Human_Camera : Camera;
    3. var Car_Camera : Camera;
    4.  
    5. function Update() {
    6.  
    7. if (Input.GetButton ("e")){
    8.     {
    9.     Human_Camera.enabled(false);
    10.     Car_Camera.enabled(true);    
    11.     }
    12.    
    13. }
    14.  
    15. if (Input.GetButton ("r")){
    16.     {
    17.     Human_Camera.enabled(true);
    18.     Car_Camera.enabled(false);    
    19.     }
    20.  
    21. }
    22.  
    and then attatch the code to the GameObject?[/quote][/code]
     
  4. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    You don't need to parent the cameras to the game object (although you can if you like); otherwise, yes.
     
  5. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    Yes, that is it. Sorry for not replying sooner to close this out, I was out at dinner.
     
unityunity