How can i disable a specific box collider of an object when i click a key in keyboard ??

How can i disable a specific box collider of an object when i click a key in keyboard ??

5 Answers

5

I’m not sure if this is old and they finally updated the Collider but you can disable or enable a collider from code pretty easily, it’s the collider.enabled bool:

if(Input.GetKey(KeyCode.E))
{
    collider.enabled = true;
}

I am using Unity 4 but I thought I saw this in 3.5 a while ago. I see some comments talking about it in duck’s answer, saying colliders don’t have an enabled bool but I just tested it and it works just fine

I think they did actually...it seems to be working for me. Thumbs up for showing how to do this!

No longer appears to work with the updated: GetComponent().SetActive = true; Setting to false however DOES work.

You can change your collider's "isTrigger" variable to true, to change it into a trigger volume instead of a solid collider. This has the effect of disabling it - in that it won't cause other objects to stop or rebound if they hit it.

For example:

function Update() {
    if (Input.GetKeyDown(KeyCode.X)) {
        collider.isTrigger = true;
    }
}

(this will disable the collider of the gameobject on which this script is placed)

doh, you're correct, although if you read what I wrote, I said "change the isTrigger var to true" - I just forgot to write that when adding the example! (fixed now).

The proposed solution does not "disable" the collider, in that if one has say a "mouseover" function on the collider, this will still work.

Well no, but it disables it for normal physics interactions - i.e. it no longer causes an object to reflect or rebound when it hits it. Since the original poster didn't describe the context, this may be enough of a solition for him. If you have a more specific requirement, perhaps you should post your own separate question giving the details.

hmmm this isnt working solution...i am looking into this since what i am trying to do is to isolate one object in a group of objects, i tried making renderer.enabled=false; but that doesnt do the trick completly since the colliders of those non-renderable objects are still active...i tried then finding all objects with GameObject.FindGameObjectsWithTag("Object"); and then setting them all to active=false; but then when i wanted to activate them again i could not find them with GameObject.FindGameObjectsWithTag("Object"); so right now i would go with disabling the colliders...

I'm using compound colliders and isTrigger causes problems when I do isTrigger=true and then back to isTrigger=false. Half the time the gameObject my colliders are children of bounces around against the other colliders in the scene.

As enabling/disabling box colliders doesn't seem to be possible, instead try just scaling the collider to 0,0,0 when you wish to disable it and then back up to it's original size when you want to enable it. Job done.

var myOldSize:Vector3;

function DisableBoxCollider(myCollider:BoxCollider)
{
    //actually just resizes it
    myOldSize=myCollider.size;
    myCollider.size=Vector3(0,0,0);
}
function EnableBoxCollider(myCollider:BoxCollider)
{
    if(myOldSize!=Vector3(0,0,0))
    myCollider.size=myOldSize;
}

collider.enabled is now supported, so you are able to use this which is quite handy

when the size of boxcollider gets decreased to (0,0,0). then the OnCollisionEnter( ) wont even detect the collison and so it wont enter the function body, so how can i resize my boxcollider as detection is no longer happening??? plz help

both “collider.isTrigger = true;” and “collider.enabled = true;” are correct, but in unity 5 you need to use GetComponent().enabled = false;

Just to make it a little clearer (because I tried around a bit and didn't read the comment above yours): You have to specify the component you want to disable between "Component" and the parentheses, e.g.: this.GetComponent<Collider2D>().enabled = false;

a question. I want to know what the correct way to check that there is no need to check every single type. chances are I'll miss a few of the types

@Grimmy

Just declare a private variable above with the collider you’re using on the same game object,
Example: PolygonCollider2D polyCol;

Then assign it to its component in the start function,
Example: polyCol = GetComponent< PolygonCollider2D > ();

And now you can use that variable to enable/ disable the collider or even set isTrigger to true or false.