How to disable collision detection while a UI Panel is enabled

Hey, I’m semi-new to Unity and I’m hoping to get some help on this issue. So I have a panel that allows the player to buy buildings, when you have enough money to buy the building it will subtract that amount of money from the player’s balance. When you click on the building it will pay a certain amount of money to the player. I am using a collider and an OnMouseDown function for this scenario, when you click on one of the buttons to try to buy the building, it senses an OnMouseDown collision (because the building is behind the button and it’s sensing a collision with the mouse). So my question is this, how do I temporarily shut off collision detection while the shop panel is open?

I’ve the same problem.

Did you solve it?

OnMouseDown is a really old callback and shouldn’t be used anylonger. Instead, you can use the UI EventSystem and implement the IPointerClickHandler interface for example. Then you can configure the GraphicRaycaster and Physics Raycaster in a way that makes UI panels block raycasts on objects behind them or the other way around.

A different approach, and also valid, would be to simply disable the EventSystem via script, to disable specific colliders in the game world, or set a global bool in some class that is respected by all other custom input code.

No approach is the best, since they all depend on what exactly you need.