Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
1
Question by Cirrocumulus · Jan 30, 2018 at 08:41 AM · interfaces

How to use Monobehaviour with an Interface

I'd like to instantiate a prefab and then check later if it has a firing script that makes it shoot at a designated target. But I'd like to pass it the target at runtime. Because Interfaces can't inherit from MonoBehaviour, I obviously can't do this:

 public interface ICanFireAtTarget {
     public AddTarget(GameObject target);
     public FireAtTarget();
 }

How can I achieve this behaviour then? How can check if an object has a script that can receive another gameObject as a target?

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

1 Reply

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

Answer by fafase · Jan 30, 2018 at 08:46 AM

First off, your interface declaration is wrong:

 using UnityEngine;
 public interface ICanFireAtTarget {
      void AddTarget(GameObject target);
      void FireAtTarget();
  }

This will get you passed the compiler error.

Then your class can implement the interface:

 public class MyClass : MonoBehaviour, ICanFireAtTarget
 {
      public void AddTarget(GameObject target){}
      public void FireAtTarget(){}
 }

and finally, since some Unity 5, interfaces can be found with GetComponent

 void OtherMethod(GameObject obj)
 {
       ICanFireAtTarget  temp = obj.GetComponent<ICanFireAtTarget >();
       if(temp != null) { temp.AddTarget(someObject); }
 }
Comment
Add comment · Show 11 · 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 Cirrocumulus · Jan 30, 2018 at 08:52 AM 0
Share

Thank you and sorry for the public/void mixup, I'm just waking up :) I didn't know I could use GameObject in an interface. Visual Studio doesn't like it at all, saying that it doesn't know what GameObject is (which makes sense). And if I try to implement the interface anyway, I get an error saying that the object doesn't implement the first interface method (the one with GameObject). I don't see how I can do what you suggest, because GameObject doesn't exist in the interface's context (as I understand it):

      public interface ICanFireAtTarget {
           void AddTarget(GameObject target);
           void FireAtTarget();
     }

avatar image fafase Cirrocumulus · Jan 30, 2018 at 09:12 AM 0
Share

On the .cs file where your interface is declared, you need

  using UnityEngine;

this will allow to use the GameObject class. Best way with interfaces to make sure you implement right, let the compiler implement for you. Add the interface to the class declaration:

  public class $$anonymous$$yClass $$anonymous$$onoBehaviour , IInterface{}

then this will trigger an error that the class does not implement the interface. Right click on IInterface and click Resolve (2nd from top, it says more but I don't remember). This will show another popup with implement explicitly. Select that, now your class has the method with exceptions so you have to implement or it will trigger on call. But at least the compiler won't complain.

avatar image Cirrocumulus fafase · Jan 30, 2018 at 09:18 AM 0
Share

Just learned something new. I didn't know I could use "using" in an interface. Thanks a lot!

Show more comments

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

75 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

Related Questions

How to call an implemented custom Interface 1 Answer

Optional actions based on Interface Implementations 0 Answers

Using interfaces as composeable plugins for a health system 1 Answer

How to create a log in user interface with Unity & Vuforia 0 Answers

All interfaces in one 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