- Home /
Detect UI Button Click Event in Update method
At present Update method of script controls movement of main player. Game HUD panel contains Pause button of game play pause functionality.
When I click on Pause UI Button, player also detects that touch and take its movement regarding that. So I want to remove this part. So in Update method if I detect this Button click event than I can ignore this irregular movement of player.
How to detect this?
Answer by LCStark · Sep 20, 2018 at 04:12 PM
Not sure if it'll work for touch input, but for mouse clicks I use EventSystem.current.IsPointerOverGameObject()
to detect when the mouse is over UI element and stop any gameplay actions.
EDIT
using UnityEngine.EventSystems;
...
void Update() {
if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject()) {
// player action
}
}
yes you are in right direction but add full code here so I can mark this as correct :)
I also require to check back - what is exact solution for this !!! But you are in right path :)
Answer by Humanhoney · Sep 20, 2018 at 02:42 PM
Create public method with boolean input variable. Add to your button EventTrigger component. On your pause button assingn this script. If you need release and pressDown assign one with "true" and other with "false". Or run method ChangePauseState.
Example:
private bool isGamePaused= false;
private void Update()
{
if(isGamePaused)
{
//Do something...
}
}
public void ChangeCurrentPauseState()
{
isGamePaused = !isGamePaused;
}
public void ChangePauseState(bool _value)
{
isGamePaused = _value;
}
Answer by vincismurf · Sep 20, 2018 at 03:37 PM
Or add a listener
public Button = but = null;
void Awake() { if(but) { but.OnClick.AddListener(OnButtonClicked); } }
public void OnButtonClicked() { Debug.Log("Button Clicked"); }
Basically, you both moving ahead into the wrong direction, I already knew how to detect the click event and detect this event in the update method.
At that time, when I was pressing the pause button, a game player also detect this input and behave based on that input.
So I want the UI detection system in this way - when I press the pause button, it only remains for that click event - not detected by the game player and change the direction.
Your answer
Follow this Question
Related Questions
Holding down UI Button and shooting raycast from touch position doesn't work simultaneously:(( 0 Answers
Button is not being clicked. 1 Answer
Only want to detect button click event 1 Answer
How to set UI button as selected on mouse over 1 Answer
How to get the fingerID (Touch) that is pressing a specific UI button. 0 Answers