- Home /
OnCollisionEnter2D not being called consistently on one object
I'm making a snake type game where when the player collides with a "food" object it will move itself to a new random location and a tail link is made. The onCollisionEnter2D will always be called on the "food" object to re-position itself but the onCollision method on the main player (which makes the new link to the snake) will only be called around 30% of the time. The player has a rigidBody and box collider while the "food" object will have a box collider.
Did you double check to make sure that the player's rigid body is non-kinematic?
Both objects are unchecked. The collision always happens. It's a matter of the player object executing its onCollision method.
I've seen other threads where onCollision wouldn't be called because they delete one object. I'm wondering if this could be the same thing as I'm moving the object out of the way. The player object might not be able to register its own collision in time. However I'm not sure what I would do to fix this.
Are you simulating physics on the food? If not, it may make more sense if it were a trigger. You may have more consistent results with OnTriggerEnter2D(Collider collider). Not sure.
The "food" having a rigidbody may also be causing its collider to move around if it's on a separate or child object. $$anonymous$$aybe it's falling, or being pushed around by other objects in your scene.
Can you post your code? In you description you have spelled "onCollisionEnter2D" incorrectly. Its supposed to be OnCollisionEnter2D with a capital O.
Your answer
![](https://koobas.hobune.stream/wayback/20220612002819im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Drawing Application: Changing Material of GameObject using OnCollisionEnter 0 Answers
How can I call OnCollisionEnter?, 1 Answer
Collision script 2 Answers