- Home /
Calling preset Button OnClick events from another script
Hi everyone. So, i have a UI Button, that makes some actions when pressed. Those actions are set by the interface, not by a code. And i have a Script, that handles pressing keys on keyboard.
I need the Script to "press" the Button (execute all commands attached to it), when a Key is pressed. Is there a way to do so? I can't find any built-in function, associated with buttons, that would handle that.
Answer by RobAnthem · Mar 20, 2017 at 02:46 AM
Also, to manually run the click event, you get the reference to the button and then you can run.
myButton.onClick.Invoke();
Yeah! That's it! Thanks a lot.
I'd mark it as an answer, but i jus don't see such an option for the commentary - only for the main post.
Thank you again, sir. And here you go: your very own "Best answer" reward!
Answer by Commoble · Mar 20, 2017 at 01:19 AM
You're looking at this from the wrong perspective: The keyboard script shouldn't activate the button, the keyboard script should activate whatever function the button also happens to activate.
For example, instead of
Keyboard script -> Button script -> Action
You should have
Button script -> Action
Keyboard script -> Action
Answer by ToyBrain101 · Mar 20, 2017 at 05:53 AM
Hello,
I think, leave the key script separate. Call scripts from the button OnClick() function. here you can add any of your scripts for the button to interact with.