How to check if a gameobjects class is implementing an interface

Two immediate ways:

  • You can use GetComponent<T>() with your interface type (useful if you only expect ONE thing expressing that interface)

  • You can get ALL the MonoBehaviours as a list (with GetComponents<MonoBehaviour>() or even GetComponentsInChildren<MonoBehaviour>()), iterate them and ask if each one IS one of your interfaces, and operate upon it accordingly.

Here’s more info:

Using Interfaces in Unity3D:

Check Youtube for other tutorials about interfaces and working in Unity3D. It’s a pretty powerful combination.

5 Likes