Enable and disable BoxCollider2D collisions

Hey,

is it possible to enable and disable the BoxCollider2D component of a gameobject so that at some point the gameobject does not detect collisions but then after it does again?

This regards a UI panel gameobject, which is only enabled at a certain event and disabled after I click a UI button inside the same UI. So event occurs > UI panel is shown > clicking button > disappears.

But now I added a BoxCollider2D to this panel which I also want to enable and disable at the same time because when the panel is shown I need the collision and can’t have them when I make it disappear.

I thought of disabling/enabling the UI panel gameobject instead of just enabling/disabling the Canvas component of the UI panel gameobject (the canvas) but I don’t really know how to access it then.

If you delete the trigger from the current gameObject that has it and put it on a new empty child gameobject, you can do something like:

public GameObject nameHere;

And then when you want to disable the collider:

nameHere.SetActive(false);

This should work a charm. Disabling a GameObject also disables the collider. Just change the false to true and put that somewhere where you close the gui. Hey also, if you want your game to freeze while the GUI is open, you can do time.TimeScale = 0 to freeze and time.TimeScale = 1 to put it back to normal too.

Hope this helps :slight_smile:

Ok so here is the thing I did, that actually worked. It’s a workaround but it totally helps with this issue:

Basically shrink the collider to (0,0) and then scale it back up when popping the UI and back to (0,0) afterwards.