Search Unity

Can't enable camera from script, but can disable it!?

Discussion in 'Scripting' started by MBE2FLSA, Jul 27, 2014.

  1. MBE2FLSA

    MBE2FLSA

    Joined:
    Jul 27, 2014
    Posts:
    2
    Camera MCam = Camera.main.GetComponent<Camera>();
    MCam.enabled = true;

    This is part of the code. If I change MCam.enabled = true to false it will work, but it will not enable the camera for some reason.
     
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Post the whole script please :)
     
  3. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Try to use:

    Code (CSharp):
    1. function Update(){
    2.     if(Input.GetKeyDown(KeyCode.E)){
    3.        MCam.enabled = !MCam.enabled;
    4.     }
    5. }
     
  4. MBE2FLSA

    MBE2FLSA

    Joined:
    Jul 27, 2014
    Posts:
    2
    Fixed the issue. I was instantiating a player and could not use Camera.main.GetComponent until it's camera component had become active. Thanks for replying.

    GameObject Player = PhotonNetwork.Instantiate ("First Person Controller", new Vector3(-29, 10, 2), Quaternion.identity, 0);

    Camera MCam = Player.GetComponent<Camera>();
    MCam.enabled = true;
     
    Sykoo likes this.