- Home /
Remove and create colliders at runtime
I am most probably just to stupid to find the answer on my question, but I didnt manage to find it. I am creating an object with collider which creates another object on OnMouseDown. This other object is created at the same position as the first one and is also meant to react to OnMouseDown. Now my problem is, that OnMouseDown is only triggered if the collider of my object is "visible" under the mouse coursor. As big parts of second objects collider are covered by the collider of the first one, I think that I have to destroy the collider of the first object. I just dindt find a way that worked. And while I am already at it, it would also great if someone could let me know on how to create that destroyed collider again.
Thanks!
I managed destroying the collider, it didnt work before as the code crashed in the line before and actually still does... I just have to call Destroy(collider);
Answer by Eric5h5 · Mar 17, 2010 at 08:32 PM
Maybe you are trying to reference the destroyed collider, which causes the crash? In any case, use AddComponent to put the collider back.
Alternately, instead of creating/destroying, put the collider on the IgnoreRaycast layer, which will cause it to be invisible to raycasts (which are used in OnMouseDown).
I didnt understand the different "objects" yesterday, as I thought that the Object returned by Instantiate is the "highest" possible and I tried to access my other scripts member variable with it, which of course didnt work as that isnt my scripts instance. Works fine with GetComponent now. Thanks for the hin with the layers, as that could get very helpfull in the future, for this I will just stick with destroying and adding again as it works fine now. Thanks for your answer.
As I am using different colliders now, I switched to the layer thingy. It works perfect and is even easier than destroying and creating again :) So thanks for that hint.