- Home /
meshcollider enable/disable
I have an odd issue. I am generating mesh colliders in script. The script works, the gameobject in the hierarchy has a mesh collider and a meshfilter.
The problem is, the collider doesn't function unless I manually disable the mesh collider then re-enable it using the inspector. Once I do that, the collider shows up in scene view (if i have disabled mesh renderer) and works how it's supposed to.
I tried to simulate in script by simply setting the enabled property on and off, but no go. Still works the same, I have to open the inspector, disable the collider, then re-enable it for it to function/display.
Any reason why this is happening?
I know this is an old question, but the same thing just started happening to me. Nothing about these object have changed since they worked, but now the colliders don't collide with my raycasts until I manually toggle them in the inspector. If anyone has a solution, I'm all ears.
Answer by MarshallN · Nov 21, 2017 at 10:19 PM
Alright, so this is a weird fix for a weird bug, but here goes: When I was instantiating the object with the MeshCollider, I enabled the MeshCollider in script just before I changed the object's parent, localScale, and rotation. When I moved the MeshCollider enable after those lines, it worked just fine. Ex:
heldObject.transform.position = wireframe.transform.position;
heldObject.GetComponent<MeshCollider>().enabled = true; // Colliders didn't work in this case.
heldObject.transform.parent = null;
heldObject.transform.localScale = Vector3.one;
heldObject.transform.eulerAngles = wireframe.transform.eulerAngles;
But if I did it the other way 'round, like this:
heldObject.transform.position = wireframe.transform.position;
heldObject.transform.parent = null;
heldObject.transform.localScale = Vector3.one;
heldObject.transform.eulerAngles = wireframe.transform.eulerAngles;
heldObject.GetComponent<MeshCollider>().enabled = true; // Here, it works.
Then it works just fine, with no weird manual toggling of the collider in the inspector.
Just curious - have you tried narrowing it down? i.e. seeing if it's all 3 of those lines that need to be before the enabling or just 1 or 2...
Just ran into this. What a nightmare. Toggling enable / disable in code fixes it, so at least I don't have to do it in Inspector. Not a very gratifying fix. I suspect there is something else going on, but I can't pinpoint it.
Answer by malkere · Apr 25, 2021 at 01:18 AM
I've had this in my game in the past and toggling enabled false then back to true worked in general... I'm running through a tutorial right now and am getting this with a scene object whose mesh I'm setting in runtime. So there's no positioning, parenting or anything, and the normal enabled toggling isn't working in frame 0 =[ I wrote a simple coroutine to disable the component, yield a frame, then re-enable it, and now it's working as intended... This is in 2019.4.14. Not sure why this seems to happen to some people and not others, but the disable/enable seems to be the only fix in some cases.