Version: Unity 6.6 (6000.6)
LanguageEnglish
  • C#

MonoBehaviour.OnCollisionExit(Collision)

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual

Parameters

Parameter Description
collision The Collision data associated with this collision.

Description

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:

  • If you disable a collider, or the GameObject it belongs to, the message would report a collider that is no longer in the simulation. Deactivating a GameObject also stops its scripts from receiving messages at all, unlike disabling a MonoBehaviour, which still receives collision events.
  • If you destroy a collider, there is nothing left to report to. The destroyed object can't receive the message, and the Collision that 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); } }