I have 10 different prefab and they falling down. When they hit collider, they will be destroy. OnCollisionEnter2D function they do not only destroy some of statement in this function. These prefabs are destroy but other statements does not run like Debug.Log(“test”);
1 Answer
1Don’t Destroy an object during a collision. Instead, create a “DestroyMe” Boolean flag, set it to true on the collision, and then when it’s true Destroy the object in Update.
This is because, if you DO destroy an object with a collider during a collision, other OnCollisionEnter2D events for that same collision will not get called (as the collider no longer exists). And since the order of calls is arbitrary, you get inconsistent results.
thanks a lot. I controlled flag to destroy object. When destroy object flag is false, then object enters the collider, flag is again true.
– abaltaHi, I use flag method to destroy prefab but I have a weird problem. While a prefab destroyed, a rest of the prefabs are destroyed same time.
– abaltaOk, finally I solve it. I checked coll.gameObject.name then set to true destroy flag.
– abaltaYou can change the physics settings to give call backs to destroyed GameObject
– Kiwasi