• 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
4
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
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

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
Add comment · 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
3

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
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 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 at 10:39 PM 0
Share

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

165 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

Related Questions

Access water4 scripts via my script? 1 Answer

Cannot destroy Component while GameObject is being activated or deactivated 2 Answers

How to activate/deactivate a component(Animator) when a specific game object was activated 0 Answers

How to add a component on a GameObject in Custom Inspector 1 Answer

AddComponent() causes a "trying to create a MonoBehaviour using the 'new' keyword" warning 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges