I’m using a CinemachineTargetGroup with a GroupComposer to frame both the main character and an enemy character that the camera is locked onto.
If the enemy character dies while the camera is active, its Transform entry in the CinemachineTargetGroup becomes invalid (of course). The game code detects when this happens and changes to a different VCam (but not always on the exact frame that the death occurs).
SOMETIMES - but not always - the destroyed Transform in the CinemachineTargetGroup results in Cinemachine throwing a MissingReferenceException during its update:
MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Transform.get_position () <0x3475124a0 + 0x00053> in <92f8bbba597a4c848e8aa06f7ebfe3ad>:0
Cinemachine.TargetPositionCache.GetTargetPosition (UnityEngine.Transform target) (at ./Library/PackageCache/com.unity.cinemachine@2.10.0/Runtime/Core/TargetPositionCache.cs:223)
Cinemachine.CinemachineTargetGroup.WeightedMemberBoundsForValidMember (Cinemachine.CinemachineTargetGroup+Target& t, UnityEngine.Vector3 avgPos, System.Single maxWeight) (at ./Library/PackageCache/com.unity.cinemachine@2.10.0/Runtime/Behaviours/CinemachineTargetGroup.cs:315)
Cinemachine.CinemachineTargetGroup.GetViewSpaceAngularBounds (UnityEngine.Matrix4x4 observer, UnityEngine.Vector2& minAngles, UnityEngine.Vector2& maxAngles, UnityEngine.Vector2& zRange) (at ./Library/PackageCache/com.unity.cinemachine@2.10.0/Runtime/Behaviours/CinemachineTargetGroup.cs:510)
Cinemachine.CinemachineGroupComposer.GetScreenSpaceGroupBoundingBox (Cinemachine.ICinemachineTargetGroup group, UnityEngine.Matrix4x4 observer, UnityEngine.Vector3& newFwd) (at ./Library/PackageCache/com.unity.cinemachine@2.10.0/Runtime/Components/CinemachineGroupComposer.cs:288)
Cinemachine.CinemachineGroupComposer.MutateCameraState (Cinemachine.CameraState& curState, System.Single deltaTime) (at ./Library/PackageCache/com.unity.cinemachine@2.10.0/Runtime/Components/CinemachineGroupComposer.cs:181)
Cinemachine.CinemachineVirtualCamera.CalculateNewState (UnityEngine.Vector3 worldUp, System.Single deltaTime) (at ./Library/PackageCache/com.unity.cinemachine@2.10.0/Runtime/Behaviours/CinemachineVirtualCamera.cs:517)
Cinemachine.CinemachineVirtualCamera.InternalUpdateCameraState (UnityEngine.Vector3 worldUp, System.Single deltaTime) (at ./Library/PackageCache/com.unity.cinemachine@2.10.0/Runtime/Behaviours/CinemachineVirtualCamera.cs:155)
Cinemachine.CinemachineCore.UpdateVirtualCamera (Cinemachine.CinemachineVirtualCameraBase vcam, UnityEngine.Vector3 worldUp, System.Single deltaTime) (at ./Library/PackageCache/com.unity.cinemachine@2.10.0/Runtime/Core/CinemachineCore.cs:361)
Cinemachine.CinemachineCore.UpdateAllActiveVirtualCameras (System.Int32 layerMask, UnityEngine.Vector3 worldUp, System.Single deltaTime) (at ./Library/PackageCache/com.unity.cinemachine@2.10.0/Runtime/Core/CinemachineCore.cs:279)
Cinemachine.CinemachineBrain.UpdateVirtualCameras (Cinemachine.CinemachineCore+UpdateFilter updateFilter, System.Single deltaTime) (at ./Library/PackageCache/com.unity.cinemachine@2.10.0/Runtime/Behaviours/CinemachineBrain.cs:493)
Cinemachine.CinemachineBrain+<AfterPhysics>d__39.MoveNext () (at ./Library/PackageCache/com.unity.cinemachine@2.10.0/Runtime/Behaviours/CinemachineBrain.cs:388)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at /Users/bokken/build/output/unity/unity/Runtime/Export/Scripting/Coroutines.cs:17)
When this occurs, it’s catastrophic: Cinemachine is in a permanently broken state and will no longer control the main camera, and the game needs to shut down.
Is this MY bug - do I need to be careful to never destroy a Transform in the CinemachineTargetGroup without first clearing its entry in the group - or is it Cinemachine’s bug?
(The nondeterministic nature of the behavior makes it feel like a Cinemachine bug. Initializing the CinemachineTargetGroup with a null Transform seems to be fine - it’s just when the Transform becomes null during runtime that this cache-related bug occasionally occurs.)
In case it’s relevant:
- The CinemachineBrain’s Update Method is “Fixed Update” (because our entities are all moved in the FixedUpdate timestep, and we don’t currently do any interpolation)
- The CinemachineTargetGroup’s Update Method is “Late Update” (arbitrarily. We could use FixedUpdate as well.)