Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

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

Discussion in 'Cinemachine' started by zeropointblack, Jul 29, 2020.

  1. zeropointblack

    zeropointblack

    Joined:
    Jun 8, 2020
    Posts:
    11
    Code (CSharp):
    1.    
    2.  
    3. var _camera = Camera.main;
    4.         var _brain = _camera.GetComponent<CinemachineBrain>();
    5.         var _vcam1st = _brain.ActiveVirtualCamera as CinemachineVirtualCamera;
    6.         var _vcam3rd = _brain.ActiveVirtualCamera as CinemachineFreeLook;
    7.  
    8.  
    9.  
    10. if (Input.GetButtonDown("RightStickButton"))
    11.         {
    12.             if (_1stCamOn == true)
    13.             {
    14.                 _1stCamOn = false;
    15.                 _vcam1st.gameObject.SetActive(false);
    16.             }
    17.             else
    18.             {
    19.                 _1stCamOn = true;
    20.                 _vcam1st.gameObject.SetActive(true);
    21.             }
    22.         }
    23.  
    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.
     
    Last edited: Jul 29, 2020
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    4,199
    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?
     
  3. zeropointblack

    zeropointblack

    Joined:
    Jun 8, 2020
    Posts:
    11
    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. public class CameraController : MonoBehaviour
    5. {
    6.     bool _1stCamOn = true;
    7.  
    8.     void Update()
    9.     {
    10.         var _camera = Camera.main;
    11.         var _brain = _camera.GetComponent<CinemachineBrain>();
    12.         var _vcam1st = _brain.ActiveVirtualCamera as CinemachineVirtualCamera;
    13.         var _vcam3rd = _brain.ActiveVirtualCamera as CinemachineFreeLook;
    14.  
    15.         //////----Change Camera----//////
    16.         if (Input.GetButtonDown("RightStickButton"))
    17.         {
    18.             if (_1stCamOn == true)
    19.             {
    20.                 _1stCamOn = false;
    21.                 _vcam1st.gameObject.SetActive(false);
    22.             }
    23.             else
    24.             {
    25.                 _1stCamOn = true;
    26.                 _vcam1st.gameObject.SetActive(true);
    27.             }
    28.         }
    29.     }
    30. }
    31.  
    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.
     
  4. CDGKen

    CDGKen

    Joined:
    Jun 28, 2019
    Posts:
    27
    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.
     
  5. zeropointblack

    zeropointblack

    Joined:
    Jun 8, 2020
    Posts:
    11
    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)


    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. public class CameraController : MonoBehaviour
    5. {
    6.     public GameObject _1stPersonCam;
    7.     private CinemachineVirtualCamera _1stCam;
    8.     bool _1stCamOn = true;
    9.  
    10.     void Start()
    11.     {
    12.         _1stCam = _1stPersonCam.GetComponent<CinemachineVirtualCamera>();
    13.  
    14.  
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         //////----Change Camera----//////
    20.         if (Input.GetButtonDown("RightStickButton"))
    21.         {
    22.             if (_1stCamOn == true)
    23.             {
    24.                 _1stCamOn = false;
    25.                 _1stCam.gameObject.SetActive(false);
    26.             }
    27.             else
    28.             {
    29.                 _1stCamOn = true;
    30.                 _1stCam.gameObject.SetActive(true);
    31.  
    32.             }
    33.         }
    34.     }
    35. }

    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.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    4,199
    Try setting this option in your vcams

    upload_2020-8-3_9-20-23.png
     
unityunity