GetActiveBrain throws an error on exit Playmode

So I use cinemachine virtual cam noise to get screen shake effect (i really didn’t like Impulse thing).
There are basically 2 scripts, one is static ScreenShake:

private static void CameraShake(float force)
    {
        GameObject go = CinemachineCore.Instance.GetActiveBrain(0).ActiveVirtualCamera.VirtualCameraGameObject;
        go.GetComponent<CinemachineVirtualCamera>().GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>().
            m_AmplitudeGain = force;
//this is how i get current active cam, no idea if there's a better way.
    }
public static void StopCameraShake() //used in RoomSwitcher script below
    {   
        DOTween.KillAll();
        CameraShake(0f);
    }

There are more things like tweening (for duration and fade ins and outs), I copied only relative stuff.

I also have several virtual cams in the scene (like rooms), so I need a way to make sure the noise stops when I enter/exit another virtual camera. The script is RoomSwitcher and it’s on every room (i’ve deleted unrelated player tag checks):

public class RoomSwitcher : MonoBehaviour
{
    [SerializeField] private CinemachineVirtualCamera virtualCamera;

    private void OnTriggerEnter2D(Collider2D other)
    {        
            virtualCamera.enabled = true;            
            CameraShakeStatic.StopCameraShake(); 
         //called from the previous script and kills all tweens and disables cam shake
    }
    private void OnTriggerExit2D(Collider2D other)
    {
            virtualCamera.enabled = false;            
            CameraShakeStatic.StopCameraShake();
     }    

It all works great, but it throws 2 errors. First one is a null ref (points to line 3 in the first script), as soon as scene starts OnTriggerEnter is triggered and my guess virtual cam isnt active yet.

i fixed it by adding this to the first script:

public static void StopCameraShake()
    {       
        if (CinemachineCore.Instance.GetActiveBrain(0).ActiveVirtualCamera == null) return;
        
        DOTween.KillAll();
        CameraShake(0f);
    }

But the 2nd error (that I cant fix) is thrown on exiting PlayMode and is Index out of range thing. Points to this method in CinemachineCore:

public CinemachineBrain GetActiveBrain(int index)
        {
            return mActiveBrains[index];
        }

It doesn’t show all the time, like about 50%, my guess it has smth to do with order of execution.
I tried guarding it by other means like:

if (CinemachineCore.Instance.BrainCount < 0) return;
or
if (CinemachineCore.Instance == null) return

Nothing worked, so I tried to disabling noise directly on that specific virtual cam (OnTriggerExit) but it didnt work out for me, i really need CURRENT active camera. The error itself doesn’t seem to affect anything, but it’s really annoying. So if anyone knows how to stop Cinemachine from looking for brains or how to stop OnTriggerExit from executing on exit Playmode, please let me know. (or if there’s any other way). Sorry for the long one.

I think I fixed it by adding a slight delay with coroutine, but still curious if there’s a better solution.

This comparison should be <= 0, I think

damn im dumb, how can a number of cameras be less than 0?

I was gonna mark it as solved, but after restarting playmode about 10 times, the error is still there.

Adding this

if (CinemachineCore.Instance.BrainCount <= 0) return;

only made it happen less often. I’ll stick to coroutines then.