- Home /
How to trigger a button click from script
I am trying to make a "Game Over" screen appear when the player health bar reaches zero. Which I was able to do... However I also want to stop the music and perform a couple other functions. I know I can do this via the script by referencing other objects, but thus far I've been using the new version 4.6+ GUI On Click() events from the Inspector which I like because they're easy to change and intuitive... How can I set up a list of events in the Inspector for when health bar reaches zero?
I've tried using a slider for the health bar, which works visually... but I only want those events to happen when the fillAmount is zero, not every time it changes.
So then I tried attaching a button to the health bar (unchecked interactable of course) and I am trying to figure out a way to trigger a button press when the fillAmount of the health bar = 0. Is it possible to trigger a button press from script? Any help would be greatly appreciated.
Here is my latest failed attempt at triggering a button press from my script:
void Update() {
if (playerHealth.fillAmount == 0) { // playerHealth is the healthbar Image.
pHealth.SendMessage ("OnClick"); // pHealth is a button attached to the image
loseScreen.SetTrigger ("youLose"); // loseScreen is a UI panel that slides in.
Time.timeScale = 0;
}
Also I realize now that even if it is possible to trigger a button from a script, I cannot do it in an update function like I have shown because it will just be pressed every frame... but there still has to be SO$$anonymous$$E way to trigger an event in the inspector from a script... is it possible to trigger one of the "Event Trigger" event types from a script?
Or does nobody know because everyone is still making GUI's the old fashioned way?
Have you had a look at the Tutorials Unity offers?
http://unity3d.com/learn/tutorials/projects/survival-shooter/game-over
On this final step of the tutorial " Project: Survival Shooter" it is an expiation of the gamer over state.
It didn't quite go over what I needed, but this was actually a super good reference in general. I checked out a couple official Unity tutorials and they were pre-4.6 so I wrote the whole tutorial section off as outdated... This video is great though and I'm definitely not writing those off from now on.
Thank you for linking this!
Answer by DiegoSLTS · Apr 13, 2015 at 07:13 PM
Make sure you have a reference to the Button component and write:
referenceToTheButton.onClick.Invoke();
http://docs.unity3d.com/ScriptReference/UI.Button.html
http://docs.unity3d.com/ScriptReference/UI.Button-onClick.html
http://docs.unity3d.com/ScriptReference/UI.Button.ButtonClickedEvent.html
http://docs.unity3d.com/ScriptReference/Events.UnityEvent.Invoke.html
Oh my god, this was too easy. >_< I thought invoke was for invoking functions on button clicks back when you had to script out each button. I had no idea it worked in reverse. Sometimes I wish Unity docs could have a couple examples for each function since the descriptions tend to be vague.
Thank you very much for your help though, this did exactly what I wanted it to do!!!
Answer by paulatwarp · Jun 29, 2018 at 06:03 PM
Using the onClick event triggers the actions on that event, but it doesn't do other button press effects such as colour changes or animations. This will send the submit message to the button's game object which is what happens when you trigger a focussed button with a keypress or game pad.
ExecuteEvents.Execute(button.gameObject, new BaseEventData(eventSystem), ExecuteEvents.submitHandler);
It's not particularly well documented. I found it by inspecting the source code for the UI.
This also works for other UI objects like Toggles that don't have onClick properties. if you do not have a variable eventSystem already you can get it with var eventSystem = EventSystem.current;
How about an OnHover Event? By the way... why submitHandler and not pointerClickHandler?
Note that you can use UnitySystem.current to get well... current event system. No need to store eventSystem variable
You need to call it with ExecuteEvents.pointerEnterHandler first in order to work.
var go = button.gameObject;
var ped = new PointerEventData(EventSystem.current);
ExecuteEvents.Execute(go, ped, ExecuteEvents.pointerEnterHandler);
ExecuteEvents.Execute(go, ped, ExecuteEvents.submitHandler);
Answer by Cyphoxia · Aug 17, 2019 at 11:12 PM
As of Unity 2019.2.1, you can Focus a button with:
yourButton.Select();
Note that it does not "click" it. Just Focus.
This might've broken in 2019.3.
Select will select the button to activate it, but does not appear to perform the click.
Answer by jakzun2011 · Jun 22, 2018 at 01:42 AM
this is what i'm working with:
if (Input.GetButton("Fire1"))
i need it to fire on a timeline so, i need a script i can enable/disable in the timeline editor
or, whatever the best suggestion is - thank you
Your answer
Follow this Question
Related Questions
Unity 5 UI Button OnClick 2 Answers
Unity 5 GUI system 1 Answer
GUI button doesn't appear on Android 0 Answers
How do i make a button appear when my FPSController walks through a tigger collider? 4 Answers
Unity 4.6 Button is not working. 4 Answers