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
0
Question by jondigarcia · Sep 04, 2021 at 09:23 AM · eventsdelegates

Can you subscribe a delegate to an event?

If I assign a local method to a delegate, I can't subscribe it to the event. Is this not possible?

samplecode.jpg (43.7 kB)
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
0
Best Answer

Answer by Bunny83 · Sep 04, 2021 at 09:36 AM

Well, it's not clear how your EAfterAttacking event is defined. However it most likely has it's own delegate type. You can not just assign different delegate types to each other. However you should be able to create a new instance of the delegate type you want to subscribe to and pass your delegate as parameter to the constructor. Since we don't know the delegate type of your event we can't really show you any code. Though for reference, see this SO question.


Please do not post code samples as an image. Concent in images can not be searched and we can't copy anything from it (besides manually retyping everything) which makes it quite difficult to provide a proper answer. Just post the code as text, select all the code text and press the 101 / 010 button to mark it as a code block.

 // just like that


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
0

Answer by jondigarcia · Sep 05, 2021 at 12:31 AM

Hi @Bunny83 , thanks for the answer! Below is the code I'm working with - apologies for me being a noob here, I can't seem to paste the text properly. Appreciate any additional inputs you may have:

     public delegate void HeroesEvent(IHero thisHero, IHero targetHero);

     public event HeroesEvent EBeforeAttacking;

     private delegate void TestMethods(IHero thisHero, IHero targetHero);

     private TestMethods _testMethods;

     private void SubscribeTest(IHero thisHero, IHero targetHero)
     {
         //If direct subscribe, there is NO error
         EBeforeAttacking += SomeLocalMethod;

         //If I try to assign the mehtod to a delegate with the same signature, I can't subscribe the delegate
         _testMethods = SomeLocalMethod;
         EBeforeAttacking += _testMethods;
     }

     private void SomeLocalMethod(IHero thisHero, IHero targetHero)
     {
     }
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 jondigarcia · Sep 05, 2021 at 12:44 AM 1
Share

@Bunny83 I already figured it out based on your answer and the reference you've provided. I should've used "HeroesEvent" and not created a new delegate type to assign the local method to.

Thanks for the help!

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

127 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

Related Questions

C Sharp Messenger Extended Warning 1 Answer

Delegate or Event: Reacting to another object's collision functions 2 Answers

Question on Events and Delegates 1 Answer

c# delegates/events using from js? 1 Answer

Unity Events, Listeners, Delegates, etc. 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