Search Unity

ActivateCameraWithDistance.cs example script error query.

Discussion in 'Cinemachine' started by LogILearn, Jul 28, 2019.

  1. LogILearn

    LogILearn

    Joined:
    Jul 28, 2019
    Posts:
    1
    Hello! I have two quick questions, I am trying to leverage the ActivateCameraWithDistance.cs script. I attach the script to a GameObject in my scene and tell it to look out for the player. I then create a new virtual camera which I use as the new camera.

    However I get the following error message twice. The code works in that once the player enters the space the camera swaps, but once he leaves the space the camera does not revert back.

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. Cinemachine.Examples.ActivateCameraWithDistance.SwitchCam (Cinemachine.CinemachineVirtualCameraBase vcam) (at Assets/CinemachineExamples/Share
    Q2. I thought I would try and repurpose the above script a little so once the player hits the X key, the camera would default back to the player camera and the new virtual camera would then be disabled. This was my code:

    Code (CSharp):
    1.   public void SwitchCam(CinemachineVirtualCameraBase vcam)
    2.     {
    3.  
    4.         if (Camera.main.GetComponent<CinemachineBrain>().ActiveVirtualCamera.Name != vcam.Name)
    5.         {
    6.             vcam.MoveToTopOfPrioritySubqueue();
    7.  
    8. // This is the part I have tried to add to disable the camera once X is pressed. But it changed nothing.
    9.             if(Input.GetKeyDown(KeyCode.X)){
    10.                 vcam.enabled = false;
    11.                 initialActiveCam.MoveToTopOfPrioritySubqueue();
    12.             }
    13.  
    14.         }
    15.     }
    Thanks everyone.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    It might be simpler to use a Collider set up as a trigger, with the CinemachineTriggerAction script attached to it. That makes it very easy to have trigger-local vams without having to write any custom code.

    As it is, it's hard to tell what's going on in your code snippets without more context.