- Home /
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
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
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.
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);
Yeah I assumed I needed to know how UnityAction class can be created.
would destroying the button that said listener is attached to also effectively remove the listener?
You should fix the code in your answer to work. Add UnityEngine.UI; and fix the typo (GetComponent with big C)
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
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);
Your answer
Follow this Question
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