How to auto remove the component that was required by another component that has been removed?

Hi all.
Let’s say I attach a script Foo to a game object. Foo automatically adds Rigidbody2D component via
[RequireComponent(typeof(Rigidbody2D))]. Later, I decide to remove the Foo component from the game object. As you can guess, Rigidbody2D will still linger to the game object even though the component Foo, which required Rigidbody2D, is gone. In this case, how do i automatically remove Rigidbody2D when I remove Foo? If Unity can’t provide a reversal method of RequireComponent, then I might be better off with adding required components at runtime, which I wouldn’t really prefer.

A little late but here is a what I did

//YOUR MONO CLASS
[requirecomponent(typeof(Banana))]
public class Up: Monobehavior
{

}

Now create a editor script for your monobehavior

        //THE EDITOR SCRIPT FOR YOUR MONO CLASS
        [CustomEditor(typeof(Up)), CanEditMultipleObjects]    
        public class UpEditor: Editor
        {
              private void OnEnable()
             {
                thisObjRef = ((Up)target).gameObject;
              }
        
            private void OnDestroy()
            {
                //The component was removed but not the gameobject
                if (target == null && thisObjRef != null)
                {
                    //Remove Dependents
                    DestroyImmediate(thisObjRef.GetComponent<Banana>());
                    Debug.Log("The component and dependent (Banana) was removed.");
                }
                else if (target == null && thisObjRef == null)
                {
                    //The gameobject was deleted
                    Debug.Log("The gameobject was deleted from the scene.");
                }
            }
    
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
         }
}

Unity - Scripting API: ExecuteInEditMode and adding a void OnDestroy with required logic should be the way

Something like this should do it

 //...
    using System;
    using System.Reflection;
    
    [RequireComponent(ComponentType)]
    public class MyClass : MonoBehaviour{
    //...
    
    void OnDestroy()
    {
            DestroyRequiredComponents(this);
    } 

//can be defined anywhere doesn't need to be in the same Class or as an extension method on MonoBehaviour
    public static void DestroyRequiredComponents(MonoBehaviour monoInstanceCaller)
    {
        MemberInfo memberInfo = monoInstanceCaller.GetType();
        RequireComponent[] requiredComponentsAtts = Attribute.GetCustomAttributes(memberInfo, typeof(RequireComponent), true) as RequireComponent[];
        foreach (RequireComponent rc in requiredComponentsAtts)
        {
            if (rc != null && monoInstanceCaller.GetComponent(rc.m_Type0) != null)
            {
                Destroy(monoInstanceCaller.GetComponent(rc.m_Type0));
            }
        }
    }
}

This will take in account only the first type parameter (m_Type0), there are 2 more so check them too if you use RequiredComponent Attribute with more than one arguments.

Cheers.

Edit: oh you meant for editor, this won’t work in this case unfortunately.