Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 _Adriaan · Oct 22, 2016 at 09:02 PM · delegatesargumentsactionseventlistener

How to add a persistent method with a single argument to a UnityEvent?

I have a UnityEvent delegate that in the inspector I can fill with zero and single-argument methods. I'm trying to do the same in code:

 UnityEngine.Events.UnityAction action = System.Delegate.CreateDelegate(typeof(UnityEngine.Events.UnityAction), false, this.GetType().GetMethod("SetActive")) as UnityEngine.Events.UnityAction;
 UnityEditor.Events.UnityEventTools.AddPersistentListener(script.unityEventDelegate, action);

...but I'm getting a 'ArgumentException: method argument length mismatch' error because UnityEvent itself is a delegate for a method without arguments? So.. how can I add single-argument methods in the inspector, and can't add them through code? What am I missing here?

FYI: I can't force the delegate on the script to take UnityEvent methods only.

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 Bunny83 · Oct 22, 2016 at 09:35 PM

There are several things wrong here. First of all "UnityAction" is a delegate without any parameters. You would need to use UnityAction<bool> as delegate type.

Second "SetActive" is an instance method but you don't pass the object reference as second parameter but "false". CreateDelegate has many overloads so make sure you read the documentation carefully.

Note that "UnityEvent" is a special Unity class that represents a serializable delegate while "UnityAction" is an actual delegate type. UnityAction has no parameters. There are several generic variants which allows you to specify one or more argument types.

edit

Well, your question is lacking some information how you actually declared your event and how you want to invoke it. There are generally two ways how a UnityEvent can be used. The non-generic UnityEvent class doesn't allow passing any parameters when it's invoked. That's why you usually can't add a listener that requires a parameter. However the UnityEvent class can wrap a delegate that requires a parameter in a special class that saves a certain parameter along with the delegate. It then provides a method without parameter which in turn calls the delegate with the stored parameter. For this you have to use:

 UnityEditor.Events.UnityEventTools.AddBoolPersistentListener().

It requires the UnityEvent, a UnityAction<bool> and the actual boolean value that should be passed to the method.

When adding such a delegate in the inspector, Unity let you enter the required parameter in an edit field.

Comment
Add comment · Show 7 · 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 22, 2016 at 09:41 PM 0
Share

ps. I'm not sure what SetActive you try to use here. If you have access to the actual class, there's no need for reflection. For example if you want to bind GameObject.SetActive you can simply do:

 UnityEngine.Events.UnityAction<bool> action = gameObject.SetActive;

Note that we assign "the method" itself so don't use the execution brackets "()" behind the method name.

avatar image _Adriaan Bunny83 · Oct 22, 2016 at 10:07 PM 0
Share

Good to know - and no, this is not about that specific function - but this unfortunately still doesn't solve the problem!

avatar image _Adriaan · Oct 22, 2016 at 10:02 PM 0
Share

I can't use UnityAction < T1 > because that would give me a compile error on the AddPersistentListener() method: it takes (UnityEvent, UnityAction) or (UnityEvent < T1 >, UnityAction < T1 >) as arguments, not (UnityEvent < T1 >, UnityAction). This is where all my problems come from.

avatar image _Adriaan · Oct 22, 2016 at 10:07 PM 0
Share

(Sorry - had to heavily edit my previous comment as the markup removed all my < T1 > things from the text for no good reason...)

avatar image Bunny83 _Adriaan · Oct 22, 2016 at 10:23 PM 0
Share

I've edited my answer. I think i finally understood what you want to do ^^.

avatar image _Adriaan Bunny83 · Oct 22, 2016 at 10:46 PM 0
Share

Ahh, there I go! Thanks a lot, Bunny83!!

avatar image LW · Nov 28, 2017 at 05:28 PM 0
Share

This was really a useful bit of information, @Bunny83 thank you!

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

59 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

Related Questions

What is Unityevent and how do they relate to Unityaction? 1 Answer

How to unsubscribe from a delegate that is inside of a singleton? 2 Answers

Subscribers of Delegates? 2 Answers

Score Workflow 1 Answer

Windows Stand-alone command line arguments. 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