Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
23
Question by RevolverOctorok · Apr 13, 2015 at 06:36 PM · unity 5guibuttonunity 4.6onclick

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;
 }
 
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RevolverOctorok · Apr 12, 2015 at 07:57 PM 0
Share

Bump so this gets seen and approved <3

avatar image RevolverOctorok · Apr 13, 2015 at 05:39 AM 0
Share

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?

avatar image Cous · Apr 13, 2015 at 06:44 PM 1
Share

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.

avatar image RevolverOctorok · Apr 13, 2015 at 11:20 PM 0
Share

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!

4 Replies

· Add your reply
  • Sort: 
avatar image
60

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RevolverOctorok · Apr 13, 2015 at 11:18 PM 2
Share

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!!!

avatar image
25

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ryuuguu · Jan 23, 2019 at 07:49 AM 0
Share

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;

avatar image sergiobd · Feb 21, 2019 at 10:57 AM 0
Share

How about an OnHover Event? By the way... why submitHandler and not pointerClickHandler?

avatar image Luchianno · Aug 24, 2019 at 09:04 PM 0
Share

Note that you can use UnitySystem.current to get well... current event system. No need to store eventSystem variable

avatar image AlexTuduran · Oct 04, 2021 at 09:22 AM 0
Share

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);
avatar image
2

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image chilton · Apr 30, 2020 at 04:34 PM 0
Share

This might've broken in 2019.3.

Select will select the button to activate it, but does not appear to perform the click.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges