How to get all components in scene which inherit a class

How to get all components from gameobjects in scene where those components inherit the same class?

For example:
FindObjectsOfType(); // I’d like it to return components of type MachineGun, GrenadeLauncher, etc. which inherit CarWeapon

For components on active GameObjects:

BaseClass[] components = GameObject.FindObjectsOfType<BaseClass>();

For all active and inactive:

BaseClass[] components = Resources.FindObjectsOfTypeAll<BaseClass>();

These will find all BaseClass components as well as derived classes. In your case BaseClass would be CarWeapon.

Active and inactive components:


    List<GameObject> rootObjectsInScene = new List<GameObject>();
    Scene scene = SceneManager.GetActiveScene();
    scene.GetRootGameObjects(rootObjectsInScene);

    for (int i = 0; i < rootObjectsInScene.Count; i++)
    {
        BaseClass[] allComponents = rootObjectsInScene*.GetComponentsInChildren<BaseClass>(true);*

for (int j = 0; j < allComponents.Length; j++)
{
allComponents[j].DoSomething();
}
}
----------
required namespaces:
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using System.Collections.Generic;

Unfortunately, it does not work with other classes than Unity Objects…
So if you want all interfaces derived classes, forget.