Is there a way to destroy a mesh collider when lets say the user clicks “m”. And then create the mesh collider again when the user clicks “m” again? Or instead of destroying and recreating ignoring then un-ignoring? I have an object that I have rendering and un-rendering when I click “m”. But when unrendered it’s still interactive.
You’d have to destroy it and then recreate it with AddComponent, you can’t disable a collider component.
You could maybe do something with Physics.IgnoreCollision(), or looping through the collision layer matrix and make the disabled objects’ layer ignore everything else; of the three, I’m not sure what would be the most performance-effective.
A similar question has been asked a few times before. Please remember to search for your questions before opening a new one.
The most pertinent example I could find is this one. For many components in Unity you can just set object.isActive = false
but apparently that doesn’t work with colliders. Instead, if you’re already disabling the renderer, you can just tell the mesh collider to ignore collisions with Physics.IgnoreCollision
or by turning the collider into a trigger (and not doing anything with OnTriggerEnter).
Better solution edit:
So I took another look around and apparently there’s a function called Physics.IgnoreLayerCollision
that works much like Physics.IgnoreCollision
except it deals with entire layers of objects. If you placed your mesh on a separate layer, when you disable the renderer, you could also just tell whatever collision you’re using to ignore the layer the mesh is on. There’s a short feature page on this thing here.
As to your mouse thing - you say you’re using OnMouseUp: so are you then raycasting? If you’re ray casting, there aren’t two objects to use (like what it sounds like you’re saying above…) but you can still alter the collision settings.
I know the question’s marked as answered now, but I thought I’d put this out here too, just in case.
Well I ended up not being able to figure out the IgnoreCollision thing though that would’ve been nice. I didn’t want to do the destroy → addcomponent because that’s just too expensive. Ended up just moving the object lightyears away then moving it back. That way I won’t be colliding with it and it’ll look like it went invisible. I guess it’s the cheapest way to do it, though it seems like a band-aid to me, which I don’t like
So this is a way to solve the problem. If anyone checks this out and can figure out how to correctly do IgnoreCollision using the mouse and an object as the two colliders do let me know. Thanks!