Reputation: 47
Why my GameObject named "pipo" is not destroyed This is my script:
private void OnTriggerEnter(Collider other) { if (other.gameObject.name == "pipo") { Destroy(other.gameObject.transform.parent.gameObject); } }
Upvotes: 4
Views: 452
Reputation: 2974
Try to change your Code a little bit, first you should generally use CompareTag()
which gives Error Messages when the given Tag doesn't exist.
After that you can add a check to see if the gameobject has a parent and depending on that destroy its parent or itself.
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("pipo")){
return;
}
if(other.gameObject.transform.parent) {
Destroy (other.gameObject.transform.parent.gameObject);
}
else {
Destroy ( other.gameObject);
}
}
When the object still doesn't get destroyed, you need to make sure that:
Upvotes: 5