Editor breaking when toggling light on and off during OnPreRender() and OnPostRender()

I was trying to create a script to make one camera see a light without another seeing it, and it works totally fine in game, but after stopping the game, the editor scene view turns completely grey and I keep getting an error message even though the game is not running, only for it to disappear once I start the game again. From my understanding, I should not be getting an error message in the editor when the game is not playing for a script that should not running at all. The game view is also entirely black after stopping the game. This wouldn’t be a problem because the game runs fine, except it makes it impossible to edit the scene once I stop running the game. Help would be appreciated, thanks!

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Rendering;

public class LightingChange : MonoBehaviour
{
public Light light;

private void Start()
{
    if (Application.isPlaying)
    {
        RenderPipelineManager.beginCameraRendering += (_, cam) =>
        {
            if (cam == gameObject.GetComponent<Camera>())
                OnPreRender();
        };
        RenderPipelineManager.endCameraRendering += (_, cam) =>
        {
            if (cam == gameObject.GetComponent<Camera>())
                OnPostRender();
        };
    }
}

void OnApplicationQuit()
{

    RenderPipelineManager.beginCameraRendering -= (_, cam) =>
    {
        if (cam == gameObject.GetComponent<Camera>())
            OnPreRender();
    };

    RenderPipelineManager.endCameraRendering -= (_, cam) =>
    {
        if (cam == gameObject.GetComponent<Camera>())
            OnPostRender();
    };
}

private void OnPreRender()
{
    if (Application.isPlaying)
    {
        light.enabled = false;
    }
}

private void OnPostRender()
{
    if (Application.isPlaying)
    {
        light.enabled = true;
    }
}

}

Also, the script runs fine without the if(Application.isPlaying) parts and the void OnApplicationQuit() part, those were just attempts at trying to fix the script