I know that I can disable rendering an object by using renderer.enabled = false, but how do I disable a collider? Surprisingly, there is no collider.enabled property, and I don't want set the entire gameObject as inactive. Does anyone know of a way to do this?
Physics.IgnoreCollision will do it.
Alternatively, you can set Collider.isTrigger to true, which doesn't entirely eliminate effects, but may do what you want. Setting isTrigger also means you don't have to ignore collisions between your object and all the other objects. Toggling this is probably faster than removing / readding the component, but you might want to test that.
If you want to remove it more permanently, you can use: Destroy
EDIT If this is already a trigger, why not just set your own custom flag on an attached monobehaviour? You can filter the state yourself in much the same way, in OnTriggerEnter, etc, just call MyScript.IsColliderEnabled. It may involve a GetComponent call in OnTrigger events depending on your setup, but this is definitely better than destroying / creating colliders.
public class MyScript : MonoBehaviour{
public bool isColliderEnabled = true;
public void OnTriggerEnter(Collider other){
if(isColliderEnabled){
//do stuff here
}
}
}
on the other collider you would just check if the collider had an attached component of type MyScript, and if so, check isColliderEnabled.
Also, consider creating a separate GameObject just for the collider and parenting it to your original object, so you can enable / disable it without enabling or disabling other components.
You guys are going to LOVE this, I’m sure! LOL
Ok,
I tried using…
collider.enabled = true;
// and
collider.enabled = false;
And although this is what the Unity Scripting reference tells you to do here…
file:///Applications/Unity/Unity.app/Contents/Documentation/Documentation/ScriptReference/Collider-enabled.html
For whatever reason I could not get this to work in my iOS game build. It worked fine while testing inside the Unity Editor, but every time I did an iOS build of my game and tested it on my iPad2, it never worked. This was incredibly frustrating and discouraging, but I finally (after hours and hours of testing with no success) I just decided to give it a rest and sleep on it. That very night I had a dream about it and I know this is going to sound CRAZY, but, here it goes… Jesus spoke to me in my dream and told me the solution to the problem…which was to just animate the Radius property of the collider. Now I know your probably falling off your chair laughing right now, but hey, at least your not crying because you can’t get your game to work! LOL
…AND what he instructed me to do in my dream actually really did work! See below…
The solution that actually worked for my game was …
- Use Unity’s built-in Animation system to animate the “Radius” property of the collider from it’s normal size to really super tiny (shrink collider)
- Create another animation that animates that same “Radius” property on the collider, but this time animate it going in the exact opposite direction (from super tiny to normal size… grow collider)
- I then appropriately named the animations … “collider shrink” and “collider grow”
- Then whenever I would normally want to use "collider.enabled = false; ", instead I just use "animation.play(“collider shrink”); “or where I would normally be inclined to use “collider.enabled = true;” I now use " animation.play(“collider grow”);”
I hope this helps someone out there who is struggling (like I was) to get their game to work. And I hope it put a smile on your face!
void Start() {
mBoxSize = (collider as BoxCollider).size;
}
public void Enable (bool isEnabled) {
(collider as BoxCollider).size = isEnabled ? mBoxSize : Vector3.zero;
}
Here’s what I’m doing. I have a class boolean variable with a getter and setter. To turn collision off I search through the gameObject for all colliders and set them accordingly:
internal var _isColliderEnabled:boolean = true;
function get isColliderEnabled():boolean
{
return _isColliderEnabled;
}
function set isColliderEnabled(value:boolean):void
{
_isColliderEnabled = value;
//get all colliders in the object
var colliders:Component[] = this.gameObject.GetComponentsInChildren(Collider);
//print(this+" isColliderEnabled="+isColliderEnabled+" colliders="+colliders+" "+colliders.Length);
var collider:Collider;
for(var i:int = 0;i < colliders.length; i++)
{
collider = colliders*.collider;*
collider.isTrigger = !value;
//print(i+") "+collider.gameObject);
}
}