Unable to enable camera

Hello,

For some reason, I am unable to activate a camera in my scene with the following script:

public class EnableDisable : MonoBehaviour
{
    public Camera cam;
    
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.C))
        {
            cam.enabled = true;
            Debug.Log("enabling camera?");
        }
    }
}

The camera is disabled when the scene begins. This script is attached to an active empty that is not in the camera hierarchy and the camera reference is in the cam field of the script component. The code is executing because I get the output “enabling camera?” every time I press C. It’s not that the camera is not rendering, I can see in the hierarchy that it’s just not active. What am I missing here? Thanks ahead :slight_smile:

@GameDev_Chuck You can load the scene with the camera disabled by disabling the component, not the gameobject. Instead of unchecking the GameObject’s active box (top left) you should uncheck the box next to the Camera component. See below:

Update: So it looks like if I have the camera set as active when the seen is loaded, then disable it in the start function of my script, then I can enable/disable as expected. But I’m still wondering why I can’t have the camera disabled on scene load then enable it during the game?

New Script:

public class EnableDisable : MonoBehaviour
{
    public Camera cam;

    void Start()
    {
        cam.enabled = false;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.C))
        {
            if (cam.enabled == false)
            {
                cam.enabled = true;
                Debug.Log("enabling camera");
            }
            else
            {
                cam.enabled = false;
                Debug.Log("disabling camera");
            }
        }
    }
}

Maybe you should try SetActive()?

You should try declaring the camera as a Game Object

public GameObject cam;

and then try to use

cam.SetActive(true)

or false when needed.