C# - Can disable a virtual camera but CANNOT re-enable it?!?!?

var _camera = Camera.main;
        var _brain = _camera.GetComponent<CinemachineBrain>();
        var _vcam1st = _brain.ActiveVirtualCamera as CinemachineVirtualCamera;
        var _vcam3rd = _brain.ActiveVirtualCamera as CinemachineFreeLook;



if (Input.GetButtonDown("RightStickButton"))
        {
            if (_1stCamOn == true)
            {
                _1stCamOn = false;
                _vcam1st.gameObject.SetActive(false);
            }
            else
            {
                _1stCamOn = true;
                _vcam1st.gameObject.SetActive(true);
            }
        }

It WORKS just fine, until I press it again, and then nothing but null reference exceptions. Same deal if I use priority to disable the 1st cam, no matter how I move the code, yeah I’m a n00b, but this should work. The only way to get it back on again in game mode is to manually reclick to enable the virtual camera. What gives? I keep “simply” disabling cameras, but then there’s no way to re-enable it. Is there a secret code to fix this? or a better way, feel free to let me know.

edit: for the record, this is a cinemachine camera setup, obviously. and switching from a first person virtual camera to a third person freelook cam and back again, or attempting to go back again.

is there a better way to do this than SetActive? seems to disable the camera from the face of the world. and as i said, changing its priority does the same. something very wrong here. a bug perhaps? Literally can’t find one web page in existence even remotely hinting at switching from first to third person camera with cinemachine. i thought that was a common thing in games.

Also, all of this code is on one script on an empty game object.

Changing the active status of a vcam won’t make it disappear - it’s the correct way to activate/deactivate. I suspect the problem lies in your surrounding code: somehow you are trashing your variables.

Can you post the whole script?

using UnityEngine;
using Cinemachine;

public class CameraController : MonoBehaviour
{
    bool _1stCamOn = true;

    void Update()
    {
        var _camera = Camera.main;
        var _brain = _camera.GetComponent<CinemachineBrain>();
        var _vcam1st = _brain.ActiveVirtualCamera as CinemachineVirtualCamera;
        var _vcam3rd = _brain.ActiveVirtualCamera as CinemachineFreeLook;

        //////----Change Camera----//////
        if (Input.GetButtonDown("RightStickButton"))
        {
            if (_1stCamOn == true)
            {
                _1stCamOn = false;
                _vcam1st.gameObject.SetActive(false);
            }
            else
            {
                _1stCamOn = true;
                _vcam1st.gameObject.SetActive(true);
            }
        }
    }
}

I’ve stripped the code down to this. Forgive the epic level of ignorance, maybe it has something to do with the variables in update? I tried moving them around but I get compiling errors. (perhaps I can ditch the bool as well, but I’ll get to that)

As you can see, the Null error jumps back and forth as I press the button, after it works beautifully the first time.

I think you’re getting those null reference exceptions because _brain.ActiveVirtualCamera can’t be casted as both a CinemachineVirtualCamera and a CinemachineFreeLook camera. It has to be one or the other since they both extend CinemachineVirtualCameraBase.

That may be the case, but I wasn’t using it in this portion of the script. However, I disabled it just in case.

Anywho, I’ve got it working now, if anyone runs into the same problem, here’s the script:
(placed on a different game object, and virtual camera dropped onto inspector public variable)

using UnityEngine;
using Cinemachine;

public class CameraController : MonoBehaviour
{
    public GameObject _1stPersonCam;
    private CinemachineVirtualCamera _1stCam;
    bool _1stCamOn = true;

    void Start()
    {
        _1stCam = _1stPersonCam.GetComponent<CinemachineVirtualCamera>();


    }

    void Update()
    {
        //////----Change Camera----//////
        if (Input.GetButtonDown("RightStickButton"))
        {
            if (_1stCamOn == true)
            {
                _1stCamOn = false;
                _1stCam.gameObject.SetActive(false);
            }
            else
            {
                _1stCamOn = true;
                _1stCam.gameObject.SetActive(true);

            }
        }
    }
}

I learned a bit more about game object scripting. I simplified this version of the code. I’m not sure what all that other junk was doing in there. Some stuff I found online. I think this is working much better.

Thanks, all, for checking, and thank you, Greg, for checking in.

Off the top of your head, if you know:

Now that the cameras are switching excellently, how about getting them to face the players direction when activated? as of now, they only rotate when they are active. So when switching, they are pointing the wrong direction most of the time. easy solution? may have something to do with my look script, but I don’t know yet. i’ll fiddle.

Try setting this option in your vcams

6163278--674130--upload_2020-8-3_9-20-23.png