Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

OnCollisionEnter2D executes 2 times on the same collision

Discussion in '2D' started by pablotilotta, May 26, 2020.

  1. pablotilotta

    pablotilotta

    Joined:
    Nov 30, 2019
    Posts:
    2
    Hi,

    I have this problem... where my Player gameObject jump and collision with another collider I see that all script code in the OnCollisionEnter2D func, runs 2 times

    How can I disable the event until the first CollisionEnter2D ends ?

    Regards

    Pablo
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    OnCollisionEnter2D is called when a unique collider/collider pair first contact. You won't get two from the same collider pair until that pair have received a OnCollisionExit2D.

    Also, if you have two Collider2D on the same GameObject you'll get the callback twice; once for each collider contacting another collider.

    It's all about collider/collider pairs though so anything beyond that needs to be done via your own logic i.e. ignore the callback upon certain conditions.
     
    Last edited: May 26, 2020
  3. pablotilotta

    pablotilotta

    Joined:
    Nov 30, 2019
    Posts:
    2
    Thanks, now works fine,

    I had the same script attached to the parent gameobject, and one of children GameObject.

    You're very kind.

    Regards

    Pablo
     
    MelvMay likes this.