x


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);

more ▼

asked Mar 17 '10 at 05:15 PM

Slin gravatar image

Slin
145 8 9 19

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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).

more ▼

answered Mar 17 '10 at 08:32 PM

Eric5h5 gravatar image

Eric5h5
80k 41 132 518

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.

Mar 18 '10 at 08:42 PM Slin

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.

Apr 12 '10 at 04:45 PM Slin
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1682
x1667
x764

asked: Mar 17 '10 at 05:15 PM

Seen: 3774 times

Last Updated: Mar 17 '10 at 07:11 PM