- Home /
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.
Answer by Jamora · Sep 18, 2013 at 03:52 PM
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.
I tried a slightly modified version of your code just for testing purposes: using UnityEngine; using System.Collections;
public class Trigger : $$anonymous$$onoBehaviour {
void OnTriggerEnter(Collider collider)
{
$$anonymous$$onoBehaviour[] list = collider.gameObject.GetComponents<$$anonymous$$onoBehaviour>();
foreach($$anonymous$$onoBehaviour mb in list)
{
if (mb is IBreakable)
{
IBreakable breakable = (IBreakable)mb;
breakable.BreakThis();
}
}
}
}
and it works perfectly indeed! I'm just not sure about performances, having to do a GetComponents upon collisions. In this very case, probably delegates (or even good ol' tagging!) will work better. But that's fine, I was just trying something to understand how Interface work and when and why use them :)
@kurai your slightly modified version works great, thanks to Jamora as well but I'm wondering if there is any other way to do it, more "cleaner" way because going through all gameobjects components seems a little bit messy. I need it to the similar Event system (OnParticleCollision) and I want to "add" interface to a parameter.
void OnParticleCollision(GameObject collision)
{
if(collision.CompareTag("Player"))
{
collision.DealDamage(100f)
}
}
And I know that collision needs to implement IDamageable to deal that damage but how to check it best way? Something like Send$$anonymous$$essage but better?
Answer by pitimoi · Sep 18, 2013 at 03:49 PM
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();
}
}
}
I don't think that works, because interfaces do not extend Component, and as such can not be fetched with GetComponent.
Uhm, this one doesn't work. I got this error: The type IBreakable' must be convertible to
UnityEngine.Component' in order to use it as parameter T' in the generic type or method
UnityEngine.GameObject.GetComponent()'
What should I do?
Your answer
Follow this Question
Related Questions
Use of Interfaces To Handle Two Object Types 2 Answers
Can't click gameobject when over another trigger? 1 Answer
How to use of interfaces and inheritance if you have seperated scripts? 0 Answers
[SOLVED] Accessing Specific Instance of Variable on Script Across Multiple Objects 0 Answers
Accessing interface in a derived class through the parent class 0 Answers