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
1
Question by UnityBrains · Oct 26, 2020 at 07:46 PM · eventdelegate

Create delegate from MethodInfo

The problem I'm facing is that I'm getting an error when creating said delegate.

It's telling me that the arguments are incompatible.

 ArgumentException: method arguments are incompatible
 System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, System.Boolean throwOnBindFailure, System.Boolean allowClosed) (at <9577ac7a62ef43179789031239ba8798>:0)
 System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method, System.Boolean throwOnBindFailure) (at <9577ac7a62ef43179789031239ba8798>:0)
 System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method) (at <9577ac7a62ef43179789031239ba8798>:0)
 EventCatcher.Start () (at Assets/EventCatcher.cs:48)

What I don't understand is that the arguments seem to be aligned. The following is the declaration of the function I want to call, the declaration of the delegate and the creation of the delegate.

 public void __Attack_SetFocused() // this is the function I want to call. (In file A)
 public delegate void OnClickFunction(); // this is the declaration of the delegate (In file B)
 OnClickFunction onClickFunction; // this is the variable I create (in file B)


The assignment of the delegate is below:

 System.Type inf = typeof(ButtonBehaviors);
 foreach (var method in inf.GetMethods())
 {
     if (method.Name == "__Attack_SetFocused")
     {
          onClickFunction = (OnClickFunction)System.Delegate.CreateDelegate(typeof(OnClickFunction), method);
     }
 }


Clearly I'm not interpreting the error correctly. If anyone can help me figure this out, I'd appreciate it.

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
1
Best Answer

Answer by UnityBrains · Oct 26, 2020 at 07:54 PM

Oh FFS. Of course JUST after I post my question I sort out what was happening. The CreateDelegate variant I'm using (`CreateDelegate(type, methodinfo)`) requires the calling function (in this case public void __Attack_SetFocused()) to be static (it's the bottom variant on this page https://docs.microsoft.com/en-us/dotnet/api/system.delegate.createdelegate?view=netcore-3.1)

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 Bunny83 · Oct 26, 2020 at 08:04 PM 0
Share

Right, either that or your delegate type requires a parameter of the type of your class (ButtonBehaviors).

Though I don't quite get why you use reflection for this? Since your method is public you can just assign it to your delegate You need a reference to your actual object instance that holds the method anyways.

avatar image UnityBrains Bunny83 · Oct 26, 2020 at 08:18 PM 0
Share

Though I don't quite get why you use reflection for this?

$$anonymous$$ainly because I wanted to. I was annoyed by the lack of control I had over Unity's input system (an example being that you can't simply get the GameObject that triggers a OnPointerEnter event - or at least I couldn't find out how) and went down a rabbit hole. $$anonymous$$y scene is set up in such a way that all of the behaviors for my buttons are in a single script, which is attached to a GameObject. Using the UI system (button for example) I'd place that object into the "OnClick" trigger and select the necessary function to fire. That works fine, until you need to know which game object the event is firing from. Because the script is attached to a game object that ISN'T the one that's triggering the event, and there's no way to intercept events, it was difficult to deter$$anonymous$$e which button was triggering a "OnPointerEnter" event. With this system, I can easily track which object triggers which 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.

Follow this Question

Answers Answers and Comments

142 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

Related Questions

When to use 'delegate', 'event' or 'Action' ? 1 Answer

Delegates versus class references 1 Answer

Event to change level 1 Answer

Login form: Do I have to set custom delegates to null in OnDestroy? 0 Answers

How do convert a lambda event subscription to be able to unsubscribe it? 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