| Parameter | Description |
|---|---|
| collision | The Collision data associated with this collision. |
OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider.
OnCollisionExit receives an instance of the Collision class as the collision parameter. This parameter contains information about the collision such as contact points and impact velocity. If you don't use this collision data in your OnCollisionExit callback, leave out the collision parameter from the method declaration to avoid unnecessary calculations.
Note: Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached. Collision events are sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions.
If one of the objects involved in the collision is deactivated or destroyed before the objects separate, OnCollisionExit isn't called. Physics backends report collision enter, stay, and exit, so Unity sends OnCollisionExit when the two colliders separate. Deactivation and destruction don't produce an exit message, for two different reasons:
MonoBehaviour, which still receives collision events.OnCollisionExit supplies describes a collider that no longer exists.Unity could send OnCollisionExit in these cases by storing and maintaining a record of every touching pair in the scene, but maintaining that record costs CPU time and memory in every project, including projects that don't need this behavior.
If your logic depends on OnCollisionExit being called whenever contact ends, including when an object is deactivated or destroyed, you need to implement additional logic, such as handling cleanup in MonoBehaviour.OnDisable or MonoBehaviour.OnDestroy, or recording contacts yourself and reconciling them each physics step. If you record contacts yourself, record each collider's EntityId, and any data you need about it, while the collider is still alive: after Unity destroys a collider, the reference to it compares equal to null, so you can't read any values from it. Subscribe to Physics.ContactEvent to read contacts from each simulation step, rather than relying on per-object messages. Unity doesn't report every contact in the scene through this event: a collider contributes contacts only if you set Collider.providesContacts to true on it, or it has a MonoBehaviour with an OnCollisionStay method attached, so enable providesContacts on the colliders you want to track. Each ContactPair identifies its colliders by ContactPair.colliderEntityId and ContactPair.otherColliderEntityId, so you can match contacts against your own records. For more information, refer to OnCollision events.OnCollisionExit can be a coroutine.
using UnityEngine; using System.Collections;
public class Example : MonoBehaviour { void OnCollisionExit(Collision other) { print("No longer in contact with " + other.transform.name); } }