Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by kurai · Sep 18, 2013 at 03:22 PM · triggerinheritanceinterface

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kurai · Sep 19, 2013 at 07:12 AM 1
Share

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 :)

avatar image Chom1czek · Oct 04, 2015 at 10:59 PM 0
Share

@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?

avatar image
1

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();
        }
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jamora · Sep 18, 2013 at 03:53 PM 0
Share

I don't think that works, because interfaces do not extend Component, and as such can not be fetched with GetComponent.

avatar image kurai · Sep 18, 2013 at 03:57 PM 0
Share

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?

avatar image Jamora · Sep 18, 2013 at 04:14 PM 0
Share

Does my answer not work either?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges