OnCollisionEnter2D calls twice

Each collision with a block makes OnCollisionEnter2D happen twice and all the methods it calls. This causes the Level class to count down the number of blocks by 2s and such. I tried working around it but couldn’t anymore when using the sprite renderer because it skips the slightly broken sprite in the array and goes straight to the really broken sprite, then on second collision causes an error from going outside the bounds of the array. Anyone know how to make it collide only once?

Hi,

Welcome to our community! :slight_smile:

Maybe there are two Block scripts attached to your block game object, or there are two block game objects on top of one another.

That doesn’t seem to be the problem. Also the OnCollisionEnter2D for the Ball calls twice each collision. Maybe I could lower the frame rate or something?

Try to set the Collision Detection of the ball’s Rigidbody2D component to “Continuous”? And log Time.frameCount into your console. Since colliders can interact with one another only once per frame, the frame number is not the same if the colliders collided twice. You could also log the name of the colliding game object and its instance ID into your console to see if the ball collides with the same collider or with different colliders. Look the type of the parameter of the OnCollisionEnter2D method up in the API to see what you can access via the variable in the parentheses.

Time.frameCount and instance ID showed that the collision event was occuring twice in the same frame on the same object. I have a weird refurbished computer may be why. I tried making it so it would only work once per frame using Time.frameCount. To do that I think it would need to get just the frame of the collision, but any value from frameCount just keeps counting frames. Do you know if this is possible? ive looked around and it sounds like there isn’t a way. Anyway, I sort of think I may have remedied the situation by adding a bool that changes on collision and only calls the hitHandle method when the bool is false. So, problem maybe solved. Thanks for the help.

Whose instance id did you log into the console? The relevant id is actually the one of the other game object, not of the ball. Maybe the ball collides with two different instance ids indicating that there are two different colliders. Then you could log the name of the other game object(s) into your console to figure out to which game objects the colliders are attached. Pass other.gameObject(or however you named the parameter in the collision method) to Debug.Log as a second argument. Then you can click on the message once, and Unity highlights the linked game object in your Hierarchy.

Without any additional information, you cannot prevent collisions from happening but you can prevent the code from being executed twice by comparing Time.frameCount to the value of an instance variable to which you assign Time.frameCount when a collision happened.

The instance ID I got was from the collision. Sorry, wasn’t clear about that. It registered the same number twice after colliding with the block then a different number twice when colliding with the paddle. I had it get the name of the collider and had the same result.
I figured out how to make it stop with the framecount value. It just needed another conditional where theFrame != frameCount. This would also work for people I read about who were having multiple random amounts of collisions, and is much better then the bool light switch thing.

Within the same frame? And does the same instance log those messages into your console? If so, that’s strange. I’m asking because it is easy to misinterpret information, for example, when you have Debug.Log in two different instances. They could log the same information into your console.

Which version of Unity do you use?

If the if-condition fixed the issue for you, keep it. :slight_smile:


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms