- Home /
Easy collision script not working, don't understand.
So I have this plane which i control around and i want it to destroy itself when it hits the ground, so once i don't want it do destroy imidiatly just for being on the ground i added a sphere collider empty object in front of the nose (i'm thinking addind two on the wings later) and i added this simple script to that:
#pragma strict
function OnCollisionEnter(col : Collision)
{
col.gameObject.SendMessageUpwards("DestroyPlane", SendMessageOptions.DontRequireReceiver);
Destroy(gameObject);
}
I then parented the plane to this tiny object and added this code to the plane control script itself:
function DestroyPlane() {
Destroy(gameObject);
}
But for some reason i don't know why the plane doesn't destroy itself when the collider hits anything.... :\
Am i missing something? Cause I can't figure out what how can the script be wrong...
No errors ? Did you checked if method DestroyPlane is beeing executed ? Is sphere collider removed ?
No errors, but that is the problem the method DestroyPlane isn't beeing executed... :(
In this case, lets this OnCollisionEnter() running for the gameObject "go1" and it collide with another gameObject "go2" then DestroyPlane is executed if its available in go2 or its parent.
Verify this and let me know if still not working :)
Answer by Doireth · Jan 24, 2013 at 06:11 PM
From documentation (http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html).
"Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached."
Are you missing a rigidbody? Also, if the sphere collider that should trigger the destruction of the object is too small then the physics engine may miss the collision (can occur if the object "would" have collided between frames).
Answer by Satamanster · Jan 25, 2013 at 10:56 AM
That's correct, i was ,missing the rigid body, not it works as it was supposed to... Thank you. ;)