- Home /
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 ?
Answer by sneftel · Apr 29, 2011 at 07:56 PM
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.
Answer by tool55 · Apr 29, 2011 at 07:59 PM
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.
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 ;)
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.
Answer by Lumart · Apr 29, 2011 at 08:08 PM
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'."
Answer by sneftel · Apr 29, 2011 at 08:55 PM
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.
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
Just like you'd keep around any other information: in a class variable for the behavior. You can use an ArrayList.
I modified my answer above using a boolean to test for the mouse click. Is this what you want?
$$anonymous$$ind of the opposite. On$$anonymous$$ouseDown
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 On$$anonymous$$ouseDown
.