- Home /
How do I go about destroying empty game objects on collision?
I am in the process of making a simply flappy bird clone, and I have the upper and lower barricade inside an empty game object. When they exit the field they trigger a collision script
public class DestroyOnContact : MonoBehaviour { void OnTriggerExit(Collider other) { Destroy (other.gameObject); } }
simple enough. Only the empty game objects I had created to store and parent the two halves continue to stack up, failing to destroy. I have tried adding a collider to the parent object. I have even tried running the collision script from the other side, having the empty game object call and destroy itself. Nothing seems to work.
Am I going about this the wrong way? Should I instead call the two halves of the boundary individually and treat them as separate objects?
thank you in advance
on the empty game object, or the two boundary halves?
I don't have one presently on the empty game object, but having added one does not solve my problem
edit
this is my script on the game object grouping the two boundarys (just a simple this.kill script) I can confirm the code works if it is placed on the individual objects, but this still leaves me with needlessly cloned empty game objects.
Is there any issue I could be having simply from both boundaries existing where there is no mesh render?
void onTriggerEnter(Collider other)
{
if (other.tag == "Boundary")
{
Destroy (this.gameObject);
}
}
Answer by Gnemlock · Mar 16, 2014 at 12:39 PM
Problem solved.
I called a collision detecting script and just referenced the items parent
Void onTriggerEnter(collider other)
{
Destroy(other.transform.parent.gameobject);
}
Treating collision off just the parent gameobject was not working because the rigidbody and the collision box were not moving in line with the children. The children also needed rigidbodies to behave correctly and i think this caused altered movement between the parent and its children.
Thanks, that helped a lot! I just started studying examples and tutorials and had the same problem: parent object wasn't destroyed.
Your answer
![](https://koobas.hobune.stream/wayback/20220613140311im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Collision between 2 prefabs. 0 Answers
How to destroy bullet on collision! 2 Answers
Collision with a coin 0 Answers
OnCollisionEnter Stopped Working 1 Answer