Search Unity

problem with prefab Instantiate and cameras

Discussion in 'Scripting' started by frigorifico, Feb 20, 2014.

  1. frigorifico

    frigorifico

    Joined:
    Sep 5, 2013
    Posts:
    37
    hi guys and girls

    i need help,

    So, i Instantiate a prefab with a couple of game objects inside namelly 4 Cameras.
    my problem is not getting the prefab or getting the cameras the problem is that whem i cicle through the cameras they change the transform value to the last camera i was in, the strangest part is that after i close the game the camera possition in the prefab are all so changed to the value of the last camera i was in, so the next time i run the game all the cameras have the same possition

    all the code i have regarding the camera problem is below
    Code (csharp):
    1.  
    2.         mapa_de_jogo = PlayerPrefs.GetString("mapa_de_jogo");      
    3.         mapa = (GameObject)Resources.Load(mapa_de_jogo, typeof(GameObject));
    4.         Instantiate(mapa, new Vector3(0, 0, 0), Quaternion.identity);
    5.  
    6.         //possicao inicial da camera
    7.         cam_pos = new  Transform[4];
    8.  
    9.  
    10.         cam_pos [CAM_INI] = mapa.transform.FindChild("Camera Inicial");
    11.         cam_pos [CAM_JOGO] = mapa.transform.FindChild("Camera Combate");
    12.         cam_pos [CAM_DIR_TURNO] = mapa.transform.FindChild("Camera Right");
    13.         cam_pos [CAM_ESQ_TURNO] = mapa.transform.FindChild("Camera Left");
    14.      
    15.         // "Camera Right" "Camera Inicial" "Camera Combate" "Camera Left"
    16.         cam_index = CAM_INI;
    17.         cam_cur_pos.position =  cam_pos[cam_index].position;
    18.         cam_cur_pos.rotation =  cam_pos[cam_index].rotation;
    19.         cam.transform.rotation = cam_cur_pos.rotation;
    20.         cam.transform.position = cam_cur_pos.position;
    21.  
    22.  

    Code (csharp):
    1.  
    2.  
    3.  void Update()
    4.     {
    5.  
    6.         if (cam_ani_inicial)
    7.         {
    8.             if (cam_index == CAM_INI)
    9.             {                
    10.                 if(cont == 64)
    11.                 {                  
    12.                     cam_index = CAM_JOGO;
    13.                 }
    14.                 cont++;
    15.             }
    16.             if(cam_cur_pos.position.y - cam_pos [CAM_JOGO].position.y <0.1)
    17.             {
    18.                 b_get_ready = true;
    19.             }
    20.             if (cam_cur_pos.position == cam_pos [CAM_JOGO].position)
    21.             {
    22.                 cam_ani_inicial = false;
    23.                 b_get_ready = false;
    24.             }
    25.         }
    26.                
    27.         //control da camera
    28.         cam_cur_pos.position = Vector3.Slerp(cam.transform.position, cam_pos [cam_index].transform.position, Time.deltaTime * smooth);
    29.         cam_cur_pos.rotation = Quaternion.Slerp(cam.transform.rotation, cam_pos [cam_index].transform.rotation, Time.deltaTime * smooth);
    30.        
    31.         cam.transform.position = cam_cur_pos.position;
    32.         cam.transform.rotation = cam_cur_pos.rotation;      
    33.     }
    34.  
    35.  
     
  2. frigorifico

    frigorifico

    Joined:
    Sep 5, 2013
    Posts:
    37
    i post my own solution its the bit of code marked by [SOLUTION]

    Code (csharp):
    1.    
    2.        mapa_de_jogo = PlayerPrefs.GetString("mapa_de_jogo");
    3.         string str = mapa_de_jogo + "(Clone)";
    4.         mapa = (GameObject)Resources.Load(mapa_de_jogo, typeof(GameObject));
    5.       [SOLUTION]  GameObject go[/SOLUTION] = Instantiate(mapa, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
    6.         //possicao inicial da camera
    7.         cam_pos = new  Transform[4];
    8.         cam_pos [CAM_INI] = go.transform.FindChild("Camera Inicial");
    9.         cam_pos [CAM_JOGO] = go.transform.FindChild("Camera Combate");
    10.         cam_pos [CAM_DIR_TURNO] = go.transform.FindChild("Camera Right");
    11.         cam_pos [CAM_ESQ_TURNO] = go.transform.FindChild("Camera Left");
    12.      
    13.         // "Camera Right" "Camera Inicial" "Camera Combate" "Camera Left"
    14.         cam_index = CAM_INI;
    15.         cam_cur_pos = cam_pos [cam_index];
    16.         cam.transform.rotation = cam_cur_pos.rotation;
    17.         cam.transform.position = cam_cur_pos.position