Cinemachine View Type Null Reference when changing / reloading scenes!

DisaronnoDog

New member
Hey, facing a particularly problematic issue at the moment...

It seems that whenever I change scenes , or reload the current scene, the Virtual Camera property of the "Third Person Cinemachine" view type in the Camera Controller becomes null; as a result, the Kinematic Object Manager's camera array also holds a null reference, throwing a NullReferenceException every tick and breaking any Kinematic characters.

This error is occurring in my scene "Level 1":

- If I run this scene directly from the editor, there is no problem, the game runs as intended; if the player dies and restarts the scene (By calling SceneManager.LoadScene(SceneManager.GetActiveScene().name)) the Null References occur.

- If I run the Main Menu Scene and load "Level 1" that way, then the Null References are thrown as soon as the scene loads for the first time.

I have played around with the Scene Execution order with no success. I have also tried implementing a method in the "CinemachineViewType" to set the m_VirtualCamera property at runtime, which works as intended, but does not fix the problem.

At this point I have bashed my head against the issue to the point of tunnel vision, and I was hoping that someone may be able to point out what I am missing!
Thanks all!

Initial Error:
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:158)

Error thrown repeatedly afterwards:
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)
 
Are you spawning the camera? The virtual camera is a regular reference so if you are spawning the camera you may need to make sure the view type holds a correct reference to the view type.
 
Are you spawning the camera? The virtual camera is a regular reference so if you are spawning the camera you may need to make sure the view type holds a correct reference to the view type.
Nope, the Main Camera and the Cinemachine virtual cameras are all active in the scene upon launch, and all references are set inside the editor. I'm sure that this has only started to happen within the last few days, but I can't think of anything that may have caused it to occur...

I think I will run the scene manager setup again, and see if it helps at all.
 
Can you revert to a previous version? If the virtual camera is set then it should be able to get a reference to it. I also recommend trying to narrow it down to a basic scene and get it working and slowly scale up from there to figure out the cause.
 
Can you revert to a previous version? If the virtual camera is set then it should be able to get a reference to it. I also recommend trying to narrow it down to a basic scene and get it working and slowly scale up from there to figure out the cause.
I have actually managed to avoid the problem for now by switching the view type to pseudo 3d rather than TP Cinemachine - This allows me to keep the functionality of Cinemachine to the extent that I need for my project while avoiding this error. I am going to say that the problem is 'solved' for now, although I am sure that if I went back to the TP Cine... view type then it would reoccur.

With regards to your response, I also thought that setting the reference prior to runtime would ensure that it is never null, regardless of loading / reloading the scene - this is why I made additions to the Cine... View Type file. However it seems that even with the property set in the editor, as well as setting it once more within Awake() / Start() of my primary level script (which I specified to run early on in the script execution order) that the error still occurs. Furthermore, if I pause Play mode the second the error pops up, the Virtual Camera property field is correctly filled. It seems that the ChangeViewType() method is called a tick or two before the field is set (which again makes no sense seeing as it is specified within the editor...). I could try and hazard a guess as to why this occurs, most likely a problem with the order that scripts are being executed, however I wouldn't be surprised if this isn't entirely my own fault. xD

Anyways, thank you as ever Justin!
 
Can you revert to a previous version? If the virtual camera is set then it should be able to get a reference to it. I also recommend trying to narrow it down to a basic scene and get it working and slowly scale up from there to figure out the cause.

I ran into the exact same problem and haven't test it with a basic scene yet.

But I think it's because m_Brain.ActiveVirtualCamera is null in ChangeViewType method of CinemachineViewType class.

Please see the red lines, m_Brain.ActiveVirtualCamera is used before the null check later.

/// <summary>
/// The view type has changed.
/// </summary>
/// <param name="activate">Should the current view type be activated?</param>
/// <param name="pitch">The pitch of the camera (in degrees).</param>
/// <param name="yaw">The yaw of the camera (in degrees).</param>
/// <param name="characterRotation">The rotation of the character.</param>
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;
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;
if (m_Brain.ActiveVirtualCamera != null) {
m_Brain.ActiveVirtualCamera.VirtualCameraGameObject.SetActive(false);
}
m_FreeLook = null;
}
}

Any ideas? Thanks!
 
Top