- Home /
[4.6 - UI] Calling function on button click via script
So I've been trying to mess around with the UI in 4.6/5.
I'm wondering how to call a function in a C# script when a UI Button has been clicked, like when trying to advanced some text been shown. I just need to find out how to check if the button has been clicked.
I have imported the UnityEngine.UI and UnityEngine.EventSystems, not sure how to use it though.
Answer by AyAMrau · Aug 24, 2014 at 11:39 PM
There are two ways around it:
Add listeners in the editor - if your button is placed in the editor and always does the same thing, then this is easiest. Select the button and find the Button component. At the bottom, there is an On Click() field. There you can select and object, script component and function on that script (function must be public), you can also add values to pass.
Add listeners in script:
[SerializeField] private Button MyButton = null; // assign in the editor
void Start() { MyButton.onClick.AddListener(() => { MyFunction(); MyOtherFunction(); });
}
The listener approach was my goal. Thanks for the helping hand!
Thanks a lot for this. I'd like to point out that when you add the new function through script, it does NOT reflect in the editor, but it does work when you click the button.
I have another question related to this. How does one assign a variable to the function call? Sort of in the same way you can assign a variable to the persistent listener. PS: I don't mean to highjack this thread, should I just ask a new question?
you can pass variables to the functions you call in the listener as normal:
$$anonymous$$yButton.onClick.AddListener(() => { $$anonymous$$yFunction("string literal"); $$anonymous$$yOtherFunction($$anonymous$$yButton.name); });
There is no variable send with the event that occurs OnClick, so all you can do is capture something from the existing objects or pass a specific value.
And yes, it would be better to make new questions if it's not strictly related to this one. I also answered quite a few UI things in last few days, so if you search there may already be an answer ;).
@LordYabo You can just use normal delegates ins$$anonymous$$d.
But the lambdas* shouldn't really cause issues here, so if you want you can post your code and we can try to spot what the problem is.
*Just for sake of correctness, this is called a lambda expression, it's widely used with Linq, but in this context Linq has nothing to do with this ;).
Answer by Aztec · Dec 04, 2014 at 01:14 PM
I was stuck at a similar problem recently. I had multiple buttons added via script and each of them should call a function with another parameter. There was a lot of trouble because everytime only the last button in the list called the function instead of the other one i really wanted to. So my trick was to add a short script (only with my listener and the variable) to each button Prefab. So if the buttons are created there is no Problem with the sharing of the listeners.
I hope i could save someone a little bit of time ^^
There are other solutions to this issue, with an explanation as to why it happens, here:
http://answers.unity3d.com/questions/791573/46-ui-how-to-apply-onclick-handler-for-button-gene.html
It's good to understand and keep this issue in $$anonymous$$d, because with ui code the listeners are widely used, so this may come up quite often.