Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
2
Question by VSZM · Dec 09, 2014 at 12:30 AM · gameobjectinterfaceobject-oriented-programming

Polymorphism and interfaces

So I have two set of game objects: Damage Dealers and Damage Takers.

The former implements this interface:

 public interface IDamageDealer
 {
     int Damage { get; set; }
 }

The latter implements this one:

 interface IDamagable 
 {
     void hitReg(IDamageDealer dd);
 }

In the OnCollisionEnter methods of the DamageDealers I want to deal damage to a Game Object if it is a Damage Taker without knowing exactly what type of Damage Taker it is.

So I added a specific tag to the Damage Takers and tried to get their script component which implements the interface IDamageDealer:

 void OnCollisionEnter(Collision collisionDetails) 
 {
     GameObject other = collisionDetails.gameObject;
     
     if(other.tag == Constants.DAMAGABLE_TAG)
     {
         other.GetComponent<IDamagable>().hitReg(this.damage);
     }
     ///...
 }

This does not work since in Unity the type parameter of this method call must be of type `UnityEngine.Component.

Is there a generic built in way to deal with this situation un Unity?

In the meantime I added an extension method to solve my problem. I hope it helps someone:

 public static class ExtensionMethods
 {
     public static T GetComponentByInterface<T>(this GameObject go) where T : class
     {
         T ret = null;
         foreach (var component in go.GetComponents<MonoBehaviour>())
         {
             T current = component as T;
             if(current != null)
             {
                 if (ret == null)
                     ret = current;
                 else
                     throw new Exception(); // TODO: OWN EXCEPTION
             }
         }

         return ret;
     }
 }
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
4
Best Answer

Answer by Bunny83 · Dec 09, 2014 at 12:50 AM

No, there isn't a generic way but you can use the non generic function. The generic GetComponent is actually just a wrapper for the System.Type version.

So instead of

 other.GetComponent<IDamagable>();

you can do

 (IDamagable)other.GetComponent(typeof(IDamagable));

As extension method it could look like this:

 public static T GetInterface<T>(this GameObject aGO) where T : class
 {
     return aGO.GetComponent(typeof(T)) as T;
 }
 public static T GetInterface<T>(this Component aComponent)  where T : class
 {
     return aComponent.GetComponent(typeof(T)) as T;
 }

I've posted them a couple of times here on UA ^^

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 Kiwasi · Dec 09, 2014 at 07:11 AM 0
Share

This is pretty good.

But since 4.6 came out, for the simple functionality described in the OP might be better served by using the event system.

avatar image VSZM · Dec 09, 2014 at 07:33 AM 0
Share

Is there a tutorial about the new event system?

avatar image Kiwasi · Dec 09, 2014 at 10:00 AM 1
Share

Not that I've found, it almost seems to have been overlooked in the release with the new UI tools. But in the long run its probably a more significant change. Goodbye Send$$anonymous$$essage.

avatar image
0

Answer by Paulius-Liekis · Dec 09, 2014 at 12:55 AM

You could do:

  public abstract class IDamageDealer : MonoBehaviour
  {
      // your abstract stuff
  }

Of course this doesn't work when you need to implement/derive more than two interfaces.

Comment
Add comment · Show 1 · 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 VSZM · Dec 09, 2014 at 06:55 AM 0
Share

Yes, that is why I don't want to use this.

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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Change GameObject According to UI selection 2 Answers

Destroyed object not comparing to null 5 Answers

Problem using IComparer to sort list of GameObjects 2 Answers

How unity works under the hood? 0 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 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