- Home /
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?
Answer by Xarbrough · Dec 18, 2020 at 10:53 PM
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.
finally I have use Time.timeScale, when I have active the canvas timeScale=1, and I can filter with this.
Your answer
Follow this Question
Related Questions
keep collider disabled even when the player re-enter the scene 1 Answer
How to block collision between selected objects 0 Answers
How do I disable all rigidbodies and colliders in a scene in unity? 2 Answers
enable disable objects when objects collides with other object 1 Answer
Pick up an item with colliders, using C# 3 Answers