- Home /
Delete 1 col.gameObject and not both
In my game I currently have a script set up that spawns 2 objects from 3 potential spawns. The problem is, two can spawn on top of each other. I want to write a method so that if that happens, one will be destroyed and the other will stay.
I currently have a method set up that I posted below that almost works, but it destroys both objects instead of just one. How can I fix it to destroy just one?
void OnCollisionEnter2D(Collision2D col){
if (col.gameObject.tag == "Enemy") {
Destroy (col.gameObject);
}
Answer by RLin · Aug 05, 2015 at 11:00 PM
You need to iterate through the contact points and destroy the corresponding other collider's GameObject. See the documentation for contact points.
I don't think that would work though since they're both the same object and they don't "enter"each other, they are already inside each other once they spawn.
How can they both be the same object? Are you saying an object collides with itself? Post screenshots of the collision.
Well two clones spawn of the original prefab. There isn't really any difference at all.
Well they spawn on top of each other. So in a sense, they're not really colliding or entering each other since the instant they spawn they're immediately overlapping.