- Home /
How to destroy two gameobjects when they collide?
I have two gameobjects which both have rigidbody2ds and boxcollider2ds set as triggers. Both also have a script which implements the OnTriggerEnter2D method. Now I want to destroy both gameobjects when they collide, but I want to do it in their respective scripts. But for some reason when the collision happens only one of them gets the callback, and even that seems a bit random. The Destroy methods documentation says that the actual destruction happens only after the next Update, so that should affect the collision handling, but somehow it does.
How have you handled this situation?
Answer by thornekey · Aug 11, 2014 at 06:32 AM
This is happening because as soon as the first one is destroyed, it cant find the second one to destroy. Destroy the objects in the same script. Its better that way.
I don't want to do that. Tested with 3D objects and with those it works the way you'd expect, but for some reason with 2D physics it doesn't.
Answer by Foose · Aug 11, 2014 at 06:37 AM
As thornekey says. But what you can do is either writing the code for destroying BOTH gameobjects in each trigger function or do a seperate script that handles all the destroying functions so it will not be destroyed when the gameobject gets destroyed.
Answer by khaled235711 · Aug 11, 2014 at 09:33 AM
Destroy the other gameObject first then destroy the current gameObject
Your answer
Follow this Question
Related Questions
Ball wont destroy in collision even though that the ball are public GameObject 3 Answers
Destroy Objects to Load New Scene 3 Answers
[JS] Destroy() just don't work :S 0 Answers
Change Score / On Click Destroy Enemy 1 Answer
Problem deleting object 1 Answer