Find a Component/GameObject Using an Interface Reference

In my script, I have a reference to an “IDamageable” interface. How would I be able to find the component that the Interface is attached to? Or the gameobject the component is attached to? Example:

public class Obj : MonoBehaviour 
{
	IDamagable<DamageInfo> damagable;

	// Use this for initialization
	void Awake () 
	{
		GameObject g = damagable.gameObject; //How would I get this?
		MonoBehaviour m = damagable.monoBehaviour; //Or this?
	}
}

Sorry if this question has been asked before, Google kept showing me unrelated questions.

Another way this can be done if you don’t want to cast would be to have the interface create a blank function like getGameObject that returns a GameObject. The implementing class would have to define what the function does and can return its gameObject.

public interface interfaceName {

     GameObject getGameObject();

}

public class className : MonoBehavior, interfaceName {

     public GameObject getGameObject() {
          return gameObject;
     }

}

An interface is not attached to something. A class can implement an interface. An interface basically acts like a seperate base class. So at any time you can cast the reference to the actual type if you know it. If all classes that implement are MonoBehaviour scripts you can always cast the reference to MonoBehaviour or Component. This gives you access to “transform” or “gameObject”. If you want to access more specific things you would need to know the actual type so you can cast the reference accordingly

The easiest way is to use an “as”-cast in conjunction with a null check.

MonoBehaviour mb = damagable as MonoBehaviour;
if (mb != null)
{
    GameObject g = mb.gameObject;
    // [...]
}

I wouldn’t normally Necro however I think this is something worth noting.

Here we specifically use lowercase gameObject.

using UnityEngine;

public interface IGetObject
{
    // This property will return the GameObject that implements this interface
    GameObject gameObject { get; }
}

This class doesn’t need to implement anything since MonoBehaviour already has a GameObject property.


public class MyClass : MonoBehaviour, IGetObject
{
    // Nothing needed here.
}

Then Finally we can use it.


public class MyOtherClass : MonoBehaviour
{
    private IGetObject myObject;

    private void Start()
    {
        // Find all MonoBehaviours in the scene, then filter for those that implement IGetObject
        IGetObject[] allObjectsWithInterface = FindObjectsByType<MonoBehaviour>(FindObjectsSortMode.None).OfType<IGetObject>().ToArray();

        if (allObjectsWithInterface.Length > 0)
        {
            // Assign the first found object that implements IGetObject
            myObject = allObjectsWithInterface[0];

            // Access the GameObject via the interface's gameObject property
            Debug.Log("Found GameObject: " + myObject.gameObject.name);
        }
        else
        {
            Debug.Log("No object implementing IGetObject was found.");
        }
    }
}

Now. As far as practices go, I don’t know if this is a great idea, but it works. This may work for other Classes like Transform too.