OnCollider(…) and OnTrigger(…): When to use them?

Is Trigger was unexpectedly false

What are these functions?

The 2 functions for managing collision between GameObjects are OnCollider and OnTrigger. These correspond to whether the collision you want to be managing of is using a collider component with “Is Trigger” set to true.

To use an OnCollider or OnTrigger function, of the 2 GameObjects involved, at least 1 needs to have a Rigidbody or Rigidbody2D and both need one of the Collider components. If you’re using one of the OnTrigger functions, one of the collider components needs to have the “Is Trigger” boolean set to true.

The “Is Trigger” boolean

OnCollider Variants: For Collider components where “Is Trigger” is false.

  • OnColliderEnter: A Unity event that is triggered once whenever 2 GameObjects with colliders where “Is Trigger” is set to false, start colliding.
  • OnColliderStay: A Unity event that is triggered once per frame whenever 2 GameObjects with colliders where “Is Trigger” is set to false, are mid-collision.
  • OnColliderExit: A Unity event that is triggered once whenever 2 GameObjects with colliders where “Is Trigger” is set to false, stop colliding.

OnTrigger Variants: For Collider components where “Is Trigger” is true.

  • OnTriggerEnter: A Unity event that is triggered once whenever 2 GameObjects with colliders, where at least one “Is Trigger” is set to true, start colliding.
  • OnTriggerStay: A Unity event that is triggered once per frame whenever 2 GameObjects with colliders, where at least one “Is Trigger” is set to true, are mid-collision.
  • OnTriggerExit: A Unity event that is triggered once whenever 2 GameObjects with colliders, where at least one “Is Trigger” is set to true, stop colliding.

A good way to think of it is:

  • Use OnCollider[…] when you want things to physically collide, and to know about it.
  • Use OnTrigger[…] when you want to know when things would have physically collided.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vincent Taylor

Vincent Taylor

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.