How do I send messages to a GameObject with an interface?

I’ve made an interface called IHoldable for GameObjects that I hope to be able to pick up during the course of my game. Here’s the definition:

public interface IHoldable {
  void WasPickedUp();
  void WasDropped();
  void WasThrown();
}

Of course, I understand that classes that implement this interface must implement all of its methods, etc. So, I have (for example) a Bomb class. Bombs can be picked up, dropped, and thrown. Here’s a sketch of the Bomb class definition.

public class Bomb : MonoBehaviour, IHoldable {
  // IHoldable implementation
  public void WasPickedUp(){}
  public void WasDropped(){}
  public void WasThrown(){}

  // other stuff
  public void Explode(){}
}

This script is then attached to a bomb GameObject. All is well and good so far.

Now, when playing the game, I press the “pick up” button, which triggers a function in the Player class that searches for nearby objects and picks up the nearest one. The function runs roughly like this:

  1. Perform a circle cast (because it’s a 2D game) to detect nearby colliders
  2. Weed out colliders whose GameObjects have no classes that implement the IHoldable interface
  3. Select the closest of the remaining colliders by comparing all of their distances
  4. Set the selected one as the heldObject variable of my player
  5. Send the WasPickedUp() message to the heldObject

My problems occur in steps 2 and 5. I don’t know how to detect whether a generic GameObject has a class that implements the IHoldable interface, and I don’t know how to send the WasPickedUp() message to an IHoldable object without knowing its class in advance or being able to check that it implements the IHoldable interface (without, of course, using Unity’s SendMessage() function, which is a little too “blind” for my purposes). I’m quite new to interfaces, so I’m sure I’m just missing something really obvious. Thanks in advance for any help!

P.S.: I’ve done a little tinkering in iOS / Objective-C, and it offers a method called respondsToSelector:(SEL)aSelector that can help in the introspection process (“selector” is Objective-C’s word for “method,” I think). Does Unity / C# offer anything like that?

Extra P.S.: I also tried calling GetComponent() on heldObject and passing in IHoldable as the type, but Unity didn’t like that at all.

The easiest to do is this GameObject.SendMessage

The more tedious approach works more like this:

foreach (var m in collider.GetComponents<MonoBehaviour>()){
  IHoldable hold = m as IHoldable;
    if (hold != null)
      hold.WasThrown();
}

If there’s only one MonoBehaviour on the GameObject leave the foreach out

A more unity way is to have Holdable as a component and use compose or subclass with Bomb. The normal way is to simple use SendMessage and let unity resolve it.

You can do it with interfeaces, but you need to create some utilities methods for that, like this one:

public static List<T> FindComponents<T>(GameObject obj) {
		System.Type type = typeof(T);
		List<T> list = new List<T>();
		Component[] components = obj.GetComponents<Component>();
		foreach (Component c in components) {
			if (type.IsAssignableFrom(c.GetType())) { 
				// cheat the compiller to transform a component into T
				object o = (object) c;
				list.Add((T) o);
			}
		}
		return list;
	}