collision2D not working - help?

Hey guys. I have a weird problem which i can’t figure out. I have set up a scene where collision occurs in a 2d scene between two gameobjects. When the collision occurs the integer variable “bigger” on one of the gameobjects changes from 0 to 1. This works perfectly when I run for the first time, but whenever i switch scenes and come back to this scene it stops working… and I have no idea why. There are 2d rigidbodies attached, and the collision occurs initially, nothing changes in the script outside of loading another level/scene, and then returning back to the scene, and then the collision stops working. I have adjusted the colliders, double checked the rigidbodies, I honestly do not know what is going on. Can someone help?

It'd help to see some code.

If you could provide more details, or better yet, some code that would help us help you a lot better :D

2 Answers

2

I don’t suppose one of the objects is destroyed or somehow deactivated in the collision, and one of the objects is persistent?

Deactivating a collider in any way (e.g. destroying the object) during collision event code prevents further events from getting called for the same collision. The order of the events is based on the object id’s, which in most cases is the order they were created in the editor (bleccch) - not something you want to rely on, and can potentially change just by reloading the scene (particularly if one of the objects isn’t destroyed during scene changes).

If this describes your problem, the solution is to not deactivate the collider during the event, but instead set a flag and deactivate it in Update.

so i figured it out after a few hours of debugging. The game objects were set to the default layer and there was a gameObject in the next scene which was scripted to ignore collisions on the default layer, I didn’t know that this script would’ve remained consistent across scenes, and this caused the collision to stop working. Everything worked when I changed the layer for the gameobjects from default to a new layer.