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

swicth camera to the start

Discussion in 'Scripting' started by xarcius, Dec 3, 2014.

  1. xarcius

    xarcius

    Joined:
    Dec 3, 2014
    Posts:
    8
    Hi
    Should I change the camera to the start
    I have seen many topics , but the solutions do not solve my problem .
    I am using these two lines of code

    Code (csharp):
    1.  
    2. cam5.camera.enabled = true ;
    3. cam1.camera.enabled = false ;
    4.  
    ( in cam1 there is MainCamera )
    I tried plugging in
    Start
    Awake
    OnEnable
    but not working
    I tried to declare them as well :ù

    Code (csharp):
    1.  
    2. Public Camera cam1 ;
    3. Public Camera cam5 ;
    4.  
    but not working
    Then I tried so

    Code (csharp):
    1.  
    2. public GameObject cam1 ;
    3. public GameObject cam5 ;
    4.  
    but not working
    I also tried with :
    Code (csharp):
    1.  
    2. Camera.main.enabled = false
    3.  
    but not working

    Can you give me some advice ?
    Thanks !

    ps: sorry for my bad english, I'm Italian
     
    Last edited: Dec 3, 2014
  2. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    public Camera cam1;
    public Camera cam5;

    So... with this, you have to do this:
    if....
    cam1.enabled=true;
    cam5.enabled=false;
    else...

    cam1.enabled=false;
    cam5.enabled=true;

    Try this
     
    xarcius and Josenifftodd like this.
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    Try this:

    Code (CSharp):
    1. cam1.SetActive(false);
    2. cam5.SetActive(true);
     
    xarcius likes this.
  4. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    This should work.
     
  5. xarcius

    xarcius

    Joined:
    Dec 3, 2014
    Posts:
    8
    thanks for the responses .
    Visiting the help page of Unity "I discovered" the depth parameter that I solved the problem . Thanks for the tips , I learned other useful ways that could help me in the future
     
  6. xarcius

    xarcius

    Joined:
    Dec 3, 2014
    Posts:
    8
    Hi all
    I have a problem again with cameras
    Use the same code for two different cameras at the start, but one works and the other does not
    (I also tried the suggestion of Zackhanzo but does not work)

    Code (csharp):
    1.  
    2. private GameObject LYLA;
    3.   private int position_after_load;
    4.   public Camera cam1;
    5.   public Camera cam6;
    6.    public Camera cam9;
    7.   public bool walk;
    8.  
    9.  
    10. void Awake(){
    11. LYLA = (GameObject)GameObject.Find("Lyla_completa_2");//player
    12. }
    13.  
    14.  
    15. void Start()
    16.   {
    17.      position_after_load = PlayerPrefs.GetInt("POSITION_AFTER_LOAD");
    18.  
    19.      if (position_after_load == 2)
    20.      {       //work
    21.        cam9.camera.depth = 0;
    22.        cam6.camera.depth = 1;
    23.      }
    24.  
    25.      if (position_after_load == 3)
    26.      {
    27.         //don't work
    28.        cam6.camera.depth=0;
    29.        cam9.camera.depth=1;
    30.      }
    31.  
    32.  
    33.   walk = false;
    34.  
    35.      switch (position_after_load)
    36.      {
    37.      case 1: break;
    38.  
    39.      case 2:
    40.        //work
    41.        walk = true;
    42.        cam6.camera.enabled = true;
    43.        cam1.camera.enabled = false;
    44.        LYLA.transform.position = new Vector3(PlayerPrefs.GetFloat("hangar_pos2_x"), PlayerPrefs.GetFloat("hangar_pos2_y"), PlayerPrefs.GetFloat("hangar_pos2_z"));
    45.        LYLA.transform.localEulerAngles = new Vector3(0f, 360f, 0f);
    46.        break;
    47.  
    48.      case 3:
    49.        //don't work
    50.        walk = true;
    51.        cam9.camera.enabled=true;
    52.        cam1.camera.enabled=false;
    53.        if(GameObject.Find("Camera9")){
    54.          Debug.Log("cam9 found");
    55.          LYLA.transform.position = new Vector3(PlayerPrefs.GetFloat("hangar_pos3_x"), PlayerPrefs.GetFloat("hangar_pos3_y"), PlayerPrefs.GetFloat("hangar_pos3_z"));
    56.          LYLA.transform.localEulerAngles = new Vector3(0f, 90f, 0f);
    57.        }
    58.        break;
    59.      }
    60.   }
    61.  

    I have this result with case 3





    EDIT:

    But Unity might have a bug to the cameras ?
    I rewrote the code , and once again the CAM9 not activated , but activates a completely different camera . Activates a camera that I use to go in first person on occasion

    Code (csharp):
    1.  
    2. public Camera cam1;
    3. public Camera cam6;
    4. public Camera cam9;
    5.  
    6. using UnityEngine;
    7. using System.Collections;
    8.  
    9. public class LOAD_HANGAR_2 : MonoBehaviour {
    10.  
    11.    public Camera cam1;
    12.    public Camera cam6;
    13.    public Camera cam9;
    14.    public GameObject LYLA;
    15.    public int position_after_load;
    16.  
    17.  
    18.    // Use this for initialization
    19.    void Start () {
    20.      position_after_load = PlayerPrefs.GetInt("POSITION_AFTER_LOAD");
    21.      switch (position_after_load) {
    22.  
    23.      case 1: break;
    24.  
    25.      case 2:
    26.          //work
    27.        cam6.camera.depth=0;
    28.        cam6.camera.enabled=true;
    29.        cam1.camera.enabled=false;
    30.        LYLA.transform.position = new Vector3(PlayerPrefs.GetFloat("hangar_pos2_x"), PlayerPrefs.GetFloat("hangar_pos2_y"), PlayerPrefs.GetFloat("hangar_pos2_z"));
    31.        LYLA.transform.localEulerAngles = new Vector3(0f, 360f, 0f);
    32.        break;
    33.  
    34.      case 3:
    35.         //don't work
    36.        cam9.camera.depth=0;
    37.        cam9.camera.enabled=true;
    38.        cam1.camera.enabled=false;
    39.        LYLA.transform.position = new Vector3(PlayerPrefs.GetFloat("hangar_pos3_x"), PlayerPrefs.GetFloat("hangar_pos3_y"), PlayerPrefs.GetFloat("hangar_pos3_z"));
    40.        LYLA.transform.localEulerAngles = new Vector3(0f, 90f, 0f);
    41.        break;
    42.      }
    43.    }
    44.  
    45. }
    46.  



    EDIT2:

    I'm trying so , but the result is not active any camera and I have a blank screen
    During the execution , in the inspector the cam9.camera is disabled despite having written
    cam9.gameobject.camera.enabled = true


    Code (csharp):
    1.  
    2.  
    3.  
    4. public GameObject cam9;
    5.    public GameObject cam1;
    6.    public GameObject camfp;
    7.  
    8. case 3:
    9.        cam9.gameObject.camera.depth=0;
    10.        cam9.gameObject.SetActive(true);
    11.        cam9.gameObject.camera.enabled=true;
    12.        camfp.gameObject.SetActive(false);
    13.        cam1.gameObject.SetActive(false);
    14.        cam1.camera.enabled=false;
    15.        LYLA.transform.position = new Vector3(PlayerPrefs.GetFloat("hangar_pos3_x"), PlayerPrefs.GetFloat("hangar_pos3_y"), PlayerPrefs.GetFloat("hangar_pos3_z"));
    16.        LYLA.transform.localEulerAngles = new Vector3(0f, 90f, 0f);
    17.        break;
    18.  
     
    Last edited: Dec 10, 2014