Prevent Input when clicking on in-game UI

PersonalPizza

New member
Hello!

I've noticed that clicking on in-game UI (such as the "main menu" button) also causes behaviors bound to "Fire1" (for example) to be invoked. Is there a way to prevent this?

My UIS setup is pretty much out-of-the-box, using the classic schema.
 
That's not specific to UIS, it's like that in any Unity game with a UI. Esentially you need to disable your players input (or whatever is using left click which is the default for "Fire1") while the panel is opened.
Some people simply set the Time.Scale to 0, but that's not a great approach. Instead in your code you should have a way to manually disable your player input. Usually you'd have a component that handles input in the Update function, and simply disabling that component does the trick. Another approach is to add a boolean check in your Update function to know whether you should check for input or not.

You can disable the player input when you pause the game or open a menu such as the Inventory.

We have an event that gets executed when a panel is opened, and another to enable/disable input. I would recommend you check out our EventNames.cs script which has a list of events which can be listened to. you can learn how to use them here:

You may want to listen to the "OnEnableGameplayInput" event (this might be wrong, I do no have the project with me since I am on holiday)

I hope that helps
 
Thanks for the reply!

I understand that I need to enable/disable player control when certain UI is open (some UI is in-game). My question was really about how to prevent clicks on a UI button from triggering an item action/behavior *before* any panel is opened. For example, in the UIS demo, if you equip a sword, then click on the in-game "main menu" button, the sword attack behavior is run as the menu is opened; ideally the sword attack behavior would not be triggered in this case. Is there an event that tiggers when any UI becomes selected (i.e. when the mouse hovers over the "main menu" button, I could disable components of my choice)?
 
Ah I see what you mean now. Unity does not have global event for UI selection. And checking every selectable in the game would be inconvenient. Usually most use check the "EventSystem.current.currentSelectedGameObject" to see if anything is selected.
 
Top