Question by
Kryloth · Feb 27, 2018 at 08:31 AM ·
collisiondestroyclonedestroy objectprefab-instance
[SOLVED] How to destroy a cloned object if it object colliding with another cloned object ?
So i have this code which Spawning an objectPrefab at a random location, sometimes they get bunched up as like spawning in the same position, i have managed to make both of them destroy itself, but i wanted to only one of them get destroyed not both of them when colliding.
Comment
Best Answer
Answer by Kryloth · Feb 28, 2018 at 04:18 AM
[SOLVED]
I got it, i destroy the object which have a lower Y axis position, so the object that's higher in Y axis wont be destroyed
void OnTriggerEnter2D(Collider2D col){
if(col.gameObject.name == "ObjectYouCollideWith" ){
if(this.gameObject.transform.position.y > col.transform.position.y){
Destroy(col.gameObject);
} else {
Destroy(this.gameObject);
}
}
Feel free to ask anything, i would be happy to help