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
0
Question by Qq0 · Jul 15, 2018 at 08:42 PM · uibuttoneditor-scriptingdelegate

Setting Button.onClick with a script in the editor

Hey, although I've seen similar questions asked and answered, I'm still completely stuck. I am trying to use UnityEventTools.AddPersistentListener to add a listener to a button in an editor script, but I get an uninformative error stating that The class null does not derive from UnityEngine.Object

I have a function that takes an integer input, and want to add it as an onClick listener as you would do in the editor

Here's my current buggy attempt (C#) . g is an int, and OpenMenuOf is a method of EnemyListControlScript

EnemyListControlScript E = GameObject.Find("EnemyListController").GetComponent(); Button btn = myButton.transform.GetChild(0).GetComponent(); UnityEventTools.AddPersistentListener(btn.onClick, new UnityAction(delegate { E.OpenMenuOf(g); }));

Not sure why this doesn't work :( any help would be much appreciated, thanks!

Comment
Add comment · Show 1
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 MezFo · Jul 15, 2018 at 09:41 PM 0
Share

You have a button in your scene and you want it to do something in a script when it is pressed? What do you mean you want to add a listener? When you add a button to your scene it should have everything you need except the part of the script you want it to run.

1 Reply

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

Answer by Qq0 · Jul 17, 2018 at 04:04 PM

After far too many attempts, I got it working, will post code here in case I can help anyone in a similar situation :/

 int g = yourInteger;  
 //E = yourInstanceOfTargetComponent  
 Button btn = yourButtonComponent;  

 System.Type[] arguments = new System.Type[1];  
 arguments[0] = typeof(int);  
 System.Reflection.MethodInfo myInfo = UnityEvent.GetValidMethodInfo(E, "OpenMenuOf", arguments);  
 UnityAction ua = System.Delegate.CreateDelegate(typeof(UnityAction), E, myInfo) as UnityAction;  

 UnityEditor.Events.UnityEventTools.AddIntPersistentListener(btn.onClick, ua, g);
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 Qq0 · Jul 17, 2018 at 04:05 PM 0
Share

PS how do you format code? ;(

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

164 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

Related Questions

add delegate to toggle in unity 4.6 UI 2 Answers

Runtime Click Listener Addition 0 Answers

Assigning a function with a parameter to buttons in for loop with a temp variable 1 Answer

Unity UI dynamic Buttons 4 Answers

Problem with onClick.AddListener 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