x


OnDestroy collision detect

Hi !

All i want to do is when the function OnDestroy is called i want to know if the object that is colliding with it has the same tag, but if i do like this:

function OnDestroy () {

   if(gameObject.collider.tag == gameObject.tag)
   {
      Destroy(Collision.collider);
   }

}

it's totally wrong right ?

HELP ?

more ▼

asked Apr 29 '11 at 07:41 PM

Lumart gravatar image

Lumart
1 1 1 5

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

4 answers: sort voted first

OnDestroy doesn't just happen, it is called after you call Object.Destroy. (Or when the scene is unloaded.) Assuming you call Object.Destroy from your OnCollisionEnter, either check the tags then, or save the Collision in a class variable so you'll have it during OnDestroy.

more ▼

answered Apr 29 '11 at 07:56 PM

sneftel gravatar image

sneftel
1.7k 7 9 20

check bellow please

Apr 29 '11 at 08:18 PM Lumart
(comments are locked)
10|3000 characters needed characters left
private var isClicked : boolean = false; 

      function OnMouseDown()
    {
    isClicked = true;
    }


     function OnCollisionEnter (other : Collision)
        {
        if (other.gameObject.tag == gameObject.tag && isClicked)
        {
        Destroy (other.gameObject);
        Destroy (gameObject);
        }
        }

Once again, haven't tested this, but using a boolean to test for a mouse click may be the simplest way to go.

more ▼

answered Apr 29 '11 at 07:59 PM

tool55 gravatar image

tool55
710 1 3 12

check bellow please

Apr 29 '11 at 08:18 PM Lumart

its better thanks !! but still doesnt work because if 2 balls collide and then they separate themselves if i click in one of them, when they get together again they destroy themselves.. i've spent the whole night with it... going to sleep now :D

I'll try again later

Thanks Anyway ;)

Apr 30 '11 at 10:23 AM Lumart

Guess I'm still not clear on what you're trying to achieve. I thought you wanted balls with the same tag to destroy themselves on collision.

May 01 '11 at 07:04 AM tool55
(comments are locked)
10|3000 characters needed characters left

let me make myself clear:

for example you have 3 blue and 3 red and 3 yellow balls all colliding. What i really want is when i click in a ball it has to check if the collider has the same tag and destroy the two or three colliders.

For example : i click in a blue ball and if there is another blue ball colliding with it they will be destroyed.

All the code that i've got in each ball it's :

function OnMouseDown () {

Destroy(gameObject);

}

function OnDestroy (){

if(gameObject.collider.tag == gameObject.tag)
{
    Destroy(Collision.collider);

}

}

And i'm getting "An instance of type 'UnityEngine.Collision' is required to access non static member 'collider'."

more ▼

answered Apr 29 '11 at 08:08 PM

Lumart gravatar image

Lumart
1 1 1 5

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

In each ball, keep a set of currently intersecting balls with the same tag. Add to this list in OnCollisionEnter and remove from it in OnCollisionExit. In OnMouseDown, delete all balls in that list.

more ▼

answered Apr 29 '11 at 08:55 PM

sneftel gravatar image

sneftel
1.7k 7 9 20

sorry but how do you "keep a set of currently intersecting balls" ? they have to be colliding so i can destroy them, for instance, if the 2 balls colide at the begining and after they decolide i cant destroy them

Apr 29 '11 at 09:08 PM Lumart

Just like you'd keep around any other information: in a class variable for the behavior. You can use an ArrayList.

Apr 29 '11 at 09:47 PM sneftel

I modified my answer above using a boolean to test for the mouse click. Is this what you want?

Apr 30 '11 at 01:07 AM tool55

Kind of the opposite. OnMouseDown is where you want to Do Things. What you need to keep around is not "whether it's clicked right now", but "what balls are intersected right now". The updating of data for use in case of clicks should happen in OnCollisionWhatever; the use of that data for destruction purposes should happen in OnMouseDown.

May 03 '11 at 05:38 PM sneftel
(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:

x2486
x2083
x1686
x120
x18

asked: Apr 29 '11 at 07:41 PM

Seen: 1265 times

Last Updated: Apr 29 '11 at 07:41 PM