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 SOME 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?
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!
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!!!
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;
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);
Bump so this gets seen and approved <3
– RevolverOctorokAlso 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 SOME 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?
– RevolverOctorokHave 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.
– CousIt 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!
– RevolverOctorok