Check if a trigger object implements an interface

Hi there.
I’m trying to experiment with Interfaces, being a total newbie with this concept.
I guess one of the advantages of using an interface is to call a method that the various objects implementing the interface would resolve in their own way.

So I made an Ibreakable interface:
using UnityEngine;
using System.Collections;

public interface IBreakable {
	
	void BreakThis();
		
}

Then I implement the interface in a script in my gameObject:

using UnityEngine;
using System.Collections;

public class Breakable : MonoBehaviour, IBreakable {
	
	public void BreakThis()
	{
		Debug.Log ("Break!");
	}
}

Ok, that should be it.

Now, I set up a trigger. What I want to do is call the BreakThis method on the object that collides with my trigger, but only if it implements the IBreakable interface. I did this:

using UnityEngine;
using System.Collections;

public class Trigger : MonoBehaviour {

	void OnTriggerEnter(Collider collider)
	{
		IBreakable testInterface = collider as IBreakable;
		
		if (testInterface!=null)
		{
			testInterface.BreakThis();
		}
	}
}

Which obviously is not working.

My question is: how can I modify the last script to correctly detect and call BreakThis on a generic IBreakable object?

Is this even a good way to use interfaces? I’m struggling understanding why and when I should use them.

You should use interfaces when you want to create a behavior, or alternatively, a role that is shared between objects, but each object might have to implement it in a different way. Using interfaces allows you to pick and choose which objects should conform to this behavior. In other words, if there is only one thing common between objects it is good to use interfaces. Because this seems to be the case here, it’s a very good application.

A further elaboration on interfaces: Consider the hierarchy on animals, you have your mammals as the top class, then classes cat, dog and sheep and what-have-you all being sub-classes (extending) mammal. It’s all well and good until scientists develop a robotic dog: it has all the attributes of a dog except it isn’t really a mammal. Extending RobotDog from Mammal wouldn’t make much sense then, right? Right. This is when we would have to start using interfaces. We need to think what roles each thing has, some could be : IBreathing, IDoglike, ICatlike etc. Now we have no problems using public class RobotDog : IDoglike.

And to answer your question, I use this function to get all scripts on a GameObject that implement a specified interface. If you end up using my function, your code would look something like:

List< IBreakable > interfaceList;
InterfaceUtility.GetInterfaces< IBreakable >(out interfaceList, collider.gameObject)
    foreach(IBreakable breakable in interfaceList){
        breakable.BreakThis();

Though if you are certain that you only ever have one IBreakable on a GameObject, you should modify it so that it only returns the interface.

Hi, what you just have to do is :

using UnityEngine;
using System.Collections;
 
public class Trigger : MonoBehaviour {
 
    void OnTriggerEnter(Collider collider)
    {
       IBreakable testInterface = collider.gameObject.GetComponent<IBreakable>();
 
       if (testInterface!=null)
       {
         testInterface.BreakThis();
       }
    }
}