+4
Answered

How to detect Button onClick

RainExplorer 3 years ago updated by gamesfan 3 years ago 14

Hi,

i want to detect button click, but i'm confused with Start and Update, where i should place component to detect onClick, at Start or Update method ?

any simple example ?

Thanks in advance...

Bolt Version:
Unity Version:
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):
GOOD, I'M SATISFIED

thanks

Satisfaction mark by RainExplorer 3 years ago
+1

you use events to detect when a button action is happening, and how it happens. you will want to usually keep it extracted from the rest of the code so it is not always asking if it is happening. You would then assign a variable if it is being held down while you start. It would look like this.



It is also beneficial to just do your logic on the on mouse input with a down action, unless you want it detected being pressed as the object is instantiated. 

Hi, 

How if i have multiple button, let's say BUTTON_A, BUTTON_B & BUTTON_C

i dont want to duplicate SET VARIABLE component.

any idea ?

and how do we know if it's Button_A / Button_B or Button_C

+1

Okay, yeah so you would do it like this then. Probably what you were looking for at the start. Right click in the graph and goto    Codebase > UnityEngine > Input and you will get an assortment of conditions if a button is pressed.


Hi, i mean get input by gameobject UI like below, because the button is from Unity'UI


How to detect, without to re-create Set Variable for each Button, because i didnt found it on CodeBase>Unity>Input


Thanks :) i'll try...

+3

Also, you can delete the start and update state, when creating a new flow graph and use UI Events like this:
https://support.ludiq.io/forums/5-bolt/topics/538-basic-ui-events/
Or you use the Unity Event...

Hi, thank you very much :)

Hi, i try to connect onClick to my Macro, but i can't found method CLICK inside it


my graph's ButtonClick :


any idea ?


Pending Review

You must choose the object that has a Flow Machine on it, not the Flow Macro itself.

+1

Ah i got it.

You have to assing the flow machine component ant not the macro asset.
You can also just assing the Gameobject, which has the flow machine on it.


Hi, Many thanks....but using this method, How do we know what button's name has clicked..

Let say, i have 100 buttons, without to create repeated same logic that need huge component...

for example, below i prefer using same event for ALL button, that's CLICK than CLICK_BUTTON_A / CLICK_BUTTON_B


Thanks in advance...

+1

Wouldn’t be easier to create a flow machine attached to a prefab? Assign this prefab to all 100 buttons and have inside a 1 single on button click event with a self variable.