Help with removing Listeners from Persistent UnityEvents via code.

I’m creating a artist friendly scene-transition (not Unity Scenes) system for a vr-game by which one can add scene transition “triggers” with connections to particular scene transitions which are created and called using Unity’s event system. These Triggers work by storing an index to a List within the SceneTransitionManager script which represents all the transitions in the game. This works fine, until a SceneTransition/UnityEvent is removed from the list, at which point all triggers which index into an element before said removal change.
For instance if a trigger references SceneTransition (/UnityEvent) at index “4”, but then SceneTransition “3” is removed from the list, the trigger now will index into what was previously SceneTransition “5”.

To remedy this I am using persistent UnityEvents so all triggers listen for a SceneTransitionRemovedEvent and modify their indexes appropriately so that they maintain their connections to the same SceneTransition (/UnityEvent).

For some reason I have not been able to get the removal of persistent events working, I have no idea why. I’m getting “NullReferenceExceptions” whenever I use methods which index into the persistent listeners. Here is my code, are the indexes not 0-based? The adding of persistent events works just fine.

Thanks!

public override void OnInspectorGUI()
    {
...
//Where "o" is the trigger/target-of-inspection....

EditorGUI.BeginChangeCheck();
            o.SceneTransitionManager =
                (SceneTransitionManager) EditorGUILayout.ObjectField(
                    o.SceneTransitionManager,
                    typeof(SceneTransitionManager),
                    true);
            if (EditorGUI.EndChangeCheck()) //scene transition manger has changed
            {
                if (o.SceneTransitionManager != null)
                {
                    int persistentEventCount = o.SceneTransitionManager.SceneTransitionRemovedEvent.GetPersistentEventCount();
                    UnityEditor.Events.UnityEventTools.AddPersistentListener(
                        unityEvent: o.SceneTransitionManager.SceneTransitionRemovedEvent,
                        call: o.OnSceneTransitionRemoved);
                  
                    o.SceneTransitionManager.SceneTransitionRemovedEvent.SetPersistentListenerState(persistentEventCount, UnityEventCallState.EditorAndRuntime);
                  
                    o.OnRemovalIndex = persistentEventCount;

                }
              
                if ( o.PreviouslyAssignedTransitionManager != null)
                {
       //NULL REFERENCE EXCEPTION OCCURS HERE
                    UnityEditor.Events.UnityEventTools.RemovePersistentListener(
                        unityEvent: o.SceneTransitionManager.SceneTransitionRemovedEvent,
                        index: o.OnRemovalIndex);

                }

                o.PreviouslyAssignedTransitionManager = o.SceneTransitionManager;

                serializedObject.ApplyModifiedProperties();
            }
...
}

When faced with a similar issue, we switched to indirection using a lookup-dictionary based on a unique ID (we used the reference of the invoking object). I’m sot sure if you can do something similar.