- Home /
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?
If you could provide more details, or better yet, some code that would help us help you a lot better :D
Answer by mealone · Sep 19, 2014 at 01:21 AM
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.
Answer by Pyrian · Sep 18, 2014 at 08:23 PM
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.
Your answer
Follow this Question
Related Questions
Collision of child is detected as collision of parent with Rigidbody2D 0 Answers
Object hits another but doesnt bounce off again. 0 Answers
How can I prevend two Circle Collider's (2D) overlapping on collision? 1 Answer
Two dynamic rigidbodies that dont conserve momentum 0 Answers
Unwanted results from 2D collisions? 1 Answer