Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
6
Question by Gurunext · Aug 26, 2014 at 07:53 PM · uibuttonupdate

Unity 4.6: GetComponent().onClick... how do you add an event to button click?

The problem is simple: new UI system has cool buttons, but if I want to really use them I need to be able to add onClick events by code. Why? Well if I know I can atach script to the button in the editor, but most of the stuff must be created after the game is started. And even if I go around it premaking all the buttons it would be nice to have just one script... not multiple scripts for every button.

in short: how do I modify/add event to this thing? GetComponent().onClick

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

6 Replies

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

Answer by AyAMrau · Aug 26, 2014 at 08:02 PM

assuming a GameObject myButton with a Button component on it:

C#

 myButton.GetComponent<Button>().onClick.AddListener(() => { someFunction(); otherFunction(); }); 

for access to UI classes such as Button add using UnityEngine.UI; at the top of the script

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 Gurunext · Aug 26, 2014 at 08:23 PM 0
Share

Yep, you have my thanks :3

To extend on my question. You've realised I had no idea how to deal with Listeners. So I would still have no idea how to use .removeListener.

avatar image AyAMrau · Aug 26, 2014 at 08:40 PM 6
Share

When you do it as above you create an anonymous function and you don't store a reference to it, so the only way you could remove it is to call RemoveAllListeners() - but that will delete all of them for the given button;

So in case you want to remove one specific listener you need to save it into a variable (in a way that can be accessed at point of removal) and then you can use the RemoveListener():

    UnityEngine.Events.UnityAction action = () => { my$$anonymous$$ethod(); myOther$$anonymous$$ethod(); };

  myButton.Getcomponent<Button>().onClick.AddListener(action);

 myButton.Getcomponent().onClick.RemoveListener(action);







avatar image Gurunext · Aug 27, 2014 at 05:40 AM 0
Share

Yeah I assumed I needed to know how UnityAction class can be created.

avatar image Frank-Grimes · Sep 28, 2014 at 09:27 PM 0
Share

would destroying the button that said listener is attached to also effectively remove the listener?

avatar image Umai · Nov 25, 2014 at 02:24 AM 1
Share

You should fix the code in your answer to work. Add UnityEngine.UI; and fix the typo (GetComponent with big C)

Show more comments
avatar image
7

Answer by Beto-Caldas · Oct 19, 2014 at 07:59 PM

Don't forget to add: using UnityEngine.UI;

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 Raynoko · Apr 02, 2015 at 11:40 AM

i have some problem, because i'm using this function, in void Update, and how can i get, only one call function? not anymore callback

 void Start()
     {
         myselfButton = GetComponent<Button>();
     }
 
     void Update () {
         if (Input.GetMouseButtonUp(0))
         {
             myselfButton.onClick.AddListener(() => actionToMaterial(index));
         }
     }

becuase, when i click on button in consoles write me any debug, but i need only one call this function. thanks

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 rahme · Aug 03, 2015 at 12:17 PM

how to add events from another scripts ?

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 endlesspeed · Oct 24, 2015 at 09:25 PM

first create class for button in my case (ToolButton.cs) then add this script like below

         currentButton = Instantiate(tool) as GameObject;
         button = currentButton.AddComponent<ToolButton>();
         button.tool = btn;
         button.counter = counter;

in ToolButton scripts on Start() function you need to add Addlistener to each button UnityEngine.Events.UnityAction action = () => { Play();}; GetComponent ().onClick.AddListener (action);

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
  • 1
  • 2
  • ›

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

29 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

Related Questions

UI Button Event Trigger Update Selected not working 0 Answers

Changing UI Buttons color 3 Answers

AddListener function not working? 6 Answers

Unity UI button priority 0 Answers

Button.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