Collision happens, code executes but not working all the time

Recording of problem (the green brick) - YouTube

I have (RigidBody2D) ball hitting (RigidBody2D) bricks. Both have Collider2D on them.
Bricks are instantiated from prefab at start, all have same “destroy” script that’s hiding them and turning colliders off since at some point I want to re-enable them again.

When ball hits them, they act as they should: they hide and keep moving. However, sometimes (more like 1 in 20 hits) brick just fails to hide. It does collide but it starts moving as rigid object instead. I tried setting it on kinematic on hit, same issue.

What I tried:

  • Both bricks and ball collision detection on continuous and descrete
  • Both bricks and ball Interpolate to interpolate / extrapolate / none
  • Bricks move with Translate() / RigidBody.AddForce() / basic transform to side
  • Tried to actually Object.destroy() bricks on collision, same thing happens

Not sure what scripts I need to put here, so basically:


Bricks move:

transform.Translate(Vector3.right * speed * Time.deltaTime);

Collision detection on bricks:

edit: for some reason collision script broke and is showing some weird symbols. I'm not home to repost it till tomorrow.

Even on times when brick is not destroyed but actually bounced back, all Debug.Log() calls show right logs: the same brick is false to renderer, false to collider.enabled and isKinematic is true so it actually executes collider script every time.

Thanks.

Edit: Video was private instead of unlisted. Fixed now.

If you’re going to disable/enable then by far the fastest way is to set Rigidbody2D.simulated to false. This tells the simulation to ignore the rigid-body, colliders, joints (etc) and doesn’t require destroying/recreating stuff. As I said, very fast.

In terms of why your bricks are not ‘hiding’; I’m not sure. As you said, you’ve verified that the script is called so it seems like it’s nothing to do with the actual physics system.