- Home /
Click a Button through a script
I've tried a bunch of different methods I've found on here to get this to work but none of them are. I've tried
Button.SendMessage("OnClick"); Button.SendMessage("OnClick", SendMessageOptions.RequireReceiver); Button.GetComponent().ClickMe;
I don't really understand how the listener aspect is supposed to work either (do I need to but the listener on a script attached to the button?). If anybody can help me understand how to use this I'd really appreciate it. Thanks!
Which button are you talking about? if its unity UI button then see this
Yeah sorry that I didn't make it clear. Its a GUI button
Answer by UltimateTactix · May 12, 2015 at 10:03 AM
I myself, whenever not using a Unity GUI button but rather created a button from scratch, add a plane to the scene and give it the text of the button and write a script for it.
The script should contain OnMousehover() as a function if you want to add something along the lines of a sound when you go over it and OnMouseDown() should contain your logic for what the button should do.
I hope I explained well enough, if not let me know and I'll provide an example script.
Tip: You can use booleans to keep track of what buttons are pressed and so on to prevent bugs.
I've also tried to execute the button's effect manually but then I run into the issue of making it non-interactable after its first use. Button.GetComponent(UnityEngine.UI.Button).interactable(false); -this doesn't work
That's easy, just add a bool to your script that's set to false by default and when you execute the script by triggering the button set it to true. Then add an if statement that no longer allows the button to function unless the bool is reset to false.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to make unity expect a click 0 Answers
Distribute terrain in zones 3 Answers
Trigger a Button Click 2 Answers
How to implement Clicking in Unity-GearVR by pointing Light??? 1 Answer