Call a method from all GameObjects implementing a specific interface in Unity my player has a gun with 3 bullets. If he runs out of ammo the scene should reset itself. I don't want to load the scene again, some components should reset themselves by using an interface.

So my observer class keeps track of the bullets and when there is nothing "active" anymore it should reset the scene.

When a bullet goes inactive it starts calling the observer.

public class Observer : MonoBehaviour
{
    [SerializeField]
    private Projectile[] projectiles; // my 3 bullets

    public void ResetScene()
    {
        for (int i = 0; i &lt; projectiles.Length; i++)
        {
            Projectile currentProjectile = projectiles[i];

            if (currentProjectile.IsActive) // Are any bullets left?
            {
                return; // Leave this method
            }
        }

        MonoBehaviour[] sceneObjects = FindObjectsOfType&lt;MonoBehaviour&gt;(); // find all objects in the scene

        for (int i = 0; i &lt; sceneObjects.Length; i++)
        {
            MonoBehaviour currentObj = sceneObjects[i];
            IRewindable currentComponent = currentObj.GetComponent&lt;IRewindable&gt;(); // try to get the interface component

            if (currentComponent != null) // does the gameobject implement this interface?
            {
                currentComponent.Rewind(); // make it rewind itself
            }
        }
    }
}


I think the code below the first loop might not be the best. Is there a more optimized way of finding all interface components and call their Rewind method?
In the first loop I think, you could do:

if (projectiles.Any(p =&gt; p.IsActive))
{
  return;
}




In the second you could do something like:

foreach (var currentComponent in sceneObjects.Select(so =&gt; so.GetComponent&lt;IRewindable&gt;()).Where(r =&gt; r != null))
{
  currentComponent.Rewind();
}


But be aware, that using linq is not necessarily the most optimized approach (performance wise) , so you'll have to do some tests.