Search Unity

Can't get Camera component even though it exists

Discussion in 'Editor & General Support' started by unity_10716629, Dec 9, 2018.

  1. unity_10716629

    unity_10716629

    Joined:
    Jun 19, 2018
    Posts:
    4
    So, basically I can't get the component Camera of my Camera GameObject.
    I've tried getting other components, like Transform, from the same GameObject, and it works normally...

    Here is a screenshot of my GameObject "CameraDead".
    upload_2018-12-9_13-0-31.png

    And here is where my code crashes:
    Code (CSharp):
    1. void respawn(){
    2. cameraDead.transform.position = new Vector3(player.transform.position.x,                    cameraDead.transform.position.y, player.transform.position.z);
    3.    
    4.         cameraDead.GetComponent<Camera>().enabled = true;//Here it crashes.
    5.         camera.GetComponent<Camera>().enabled = false;
    6.         player.transform.SetPositionAndRotation(new Vector3(-27f, 0f, -8.35f), Quaternion.Euler(new Vector3(0,90,0)));
    7.         isDead = true;
    8.     }
    Thanks in advance
     
  2. What does it means 'crashes'?
     
  3. unity_10716629

    unity_10716629

    Joined:
    Jun 19, 2018
    Posts:
    4
    Sorry for not explaning it before.
    When I try to get the Camera component it returns null, so this causes the code to crash when I try to disable the Camera component.