Error Updating Third Person Ultimate Character Controller

KO_Games

Member
Hi,

I just updated to version 2.4.5 and I'm using the cinemachine integration. I downloaded the most recent cinemachine integration as well. However, I'm getting null reference errors now in my game that were not there before:

NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.Integrations.Cinemachine.CinemachineViewType.ChangeViewType (System.Boolean activate, System.Single pitch, System.Single yaw, UnityEngine.Quaternion characterRotation) (at Assets/Opsive/UltimateCharacterController/Integrations/Cinemachine/CinemachineViewType.cs:156)
Opsive.UltimateCharacterController.Camera.CameraController.InitializeCharacter (UnityEngine.GameObject character) (at Assets/Opsive/UltimateCharacterController/Scripts/Camera/CameraController.cs:468)
Opsive.UltimateCharacterController.Camera.CameraController.Start () (at Assets/Opsive/UltimateCharacterController/Scripts/Camera/CameraController.cs:403)

NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.Game.KinematicObjectManager.Update () (at Assets/Opsive/UltimateCharacterController/Scripts/Game/KinematicObjectManager.cs:833)

I attached a copy of my setup for the camera controller script. I tried to use the debugger and I put in line breaks, but I can't figure out what might be the issue.

Any help would be greatly appreciated! Thanks!
 

Attachments

  • Screenshot 2022-01-31 020215.png
    Screenshot 2022-01-31 020215.png
    70.4 KB · Views: 5
It looks like the m_Brain.ActiveVirtualCamera is fine the first time I enter the scene, but if I restart the scene it becomes null when the line 156 of the code is excecuted in CinemachineViewType script. I think the issue might be that this line of code is getting called in awake and according to this Unity forum, you don't want get the active Virtual Camera that early or else it might not be initialized yet.


I wonder if there is some check that could be done to make sure that the ActiveVirtualCamera has been initialized first? Or maybe something that we could do with script execution order?
1643696818526.png

Could be something else too...but that is just what I found. What do you think?
 
On further inspection of the code I found what is causing the problem. See the two commented-out lines. If I remove those two lines then everything works without errors. Do you know if removing these two lines will cause other issues? Or is there better way to solve the problem?

C#:
public override void ChangeViewType(bool activate, float pitch, float yaw, Quaternion characterRotation)
        {
            m_Brain.enabled = activate;
            if (activate) {
                //m_VirtualCamera.Priority = m_Brain.ActiveVirtualCamera.Priority + 1; //This line causes null errors on scene reload due to active virtual camera being null
                m_Pitch = pitch;
                if (m_Camera.fieldOfView != m_FieldOfView) {
                    m_FieldOfViewChangeTime = Time.time + m_FieldOfViewDamping / m_CharacterLocomotion.TimeScale;
                }

                // A virtual camera will not exist when Cinemachine first starts.
                if (m_Brain.ActiveVirtualCamera == null) {
                    m_Brain.m_CameraActivatedEvent.AddListener(ActivatedVirtualCamera);
                } else {
                    UpdateFreeLookCameraValues();
                }
            } else {
                //m_VirtualCamera.Priority = m_Brain.ActiveVirtualCamera.Priority - 1; //This line causes null errors on scene reload due to active virtual camera being null
                if (m_Brain.ActiveVirtualCamera != null) {
                    m_Brain.ActiveVirtualCamera.VirtualCameraGameObject.SetActive(false);
                }
                m_FreeLook = null;
            }
        }
 
Hi Justin,

I know you are super busy. I just wanted to follow up on my previous response to see what your thoughts were on this solution. Do you forsee any issues with the changes I made to the code by commenting out those two lines?
 
Unfortunately I am not familiar enough with Cinemachine to know why the virtual camera would be null, but I am glad that it's working for you!
 
Unfortunately I am not familiar enough with Cinemachine to know why the virtual camera would be null, but I am glad that it's working for you!
No worries. I just wanted to check with you since the issue was on the Opsive integration script for Cinemachine.
 
Top