• 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
Question by JoshDangIt · Jun 23, 2016 at 08:16 PM · c#gameobjectcomponentinterface

Find a Component/GameObject Using an Interface Reference

In my script, I have a reference to an "IDamageable" interface. How would I be able to find the component that the Interface is attached to? Or the gameobject the component is attached to? Example:

 public class Obj : MonoBehaviour 
 {
     IDamagable<DamageInfo> damagable;
 
     // Use this for initialization
     void Awake () 
     {
         GameObject g = damagable.gameObject; //How would I get this?
         MonoBehaviour m = damagable.monoBehaviour; //Or this?
     }
 }


Sorry if this question has been asked before, Google kept showing me unrelated questions.

Comment
Bunny83
Hunter_Bobeck
Akhrand
Mobazy
NeatWolf
piers_audiomob

People who like this

6 Show 0
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

  • Sort: 
avatar image

Answer by merckerrobert · Dec 03, 2018 at 10:10 PM

Another way this can be done if you don't want to cast would be to have the interface create a blank function like getGameObject that returns a GameObject. The implementing class would have to define what the function does and can return its gameObject.

 public interface interfaceName {
 
      GameObject getGameObject();
 
 }
 
 public class className : MonoBehavior, interfaceName {
 
      public GameObject getGameObject() {
           return gameObject;
      }
 
 }
Comment
TadasTalalas
kendallroth
Filip8429
Mobazy
dARTillery
mossibat
piers_audiomob
fax58

People who like this

8 Show 0 · 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

Answer by Bunny83 · Jun 23, 2016 at 08:48 PM

An interface is not attached to something. A class can implement an interface. An interface basically acts like a seperate base class. So at any time you can cast the reference to the actual type if you know it. If all classes that implement are MonoBehaviour scripts you can always cast the reference to MonoBehaviour or Component. This gives you access to "transform" or "gameObject". If you want to access more specific things you would need to know the actual type so you can cast the reference accordingly

The easiest way is to use an "as"-cast in conjunction with a null check.

 MonoBehaviour mb = damagable as MonoBehaviour;
 if (mb != null)
 {
     GameObject g = mb.gameObject;
     // [...]
 }

Comment
bellicapax
St2wart
rtoole13
SorraTheOrc
NeatWolf
piers_audiomob

People who like this

6 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 travelhawk · Oct 09, 2019 at 08:28 AM 0
Share

Nice solution. I struggled with this and this solves my problem. Thanks!

avatar image jyblackshaw · Feb 05, 2021 at 10:39 PM 0
Share

Do you know a good way of tagging classes that could reference something like an event.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

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

Related Questions

Distribute terrain in zones 3 Answers

Disabling A Script on a GameObject From a Different Script 2 Answers

Classes and Scripting 2 Answers

Add component to a gameobject created through code. 1 Answer

Access water4 scripts via my script? 1 Answer


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