- Home /
Destroy Clone when colliding with 2d box collider
Her is my script, I have it set to look for the tag and destroy when that clone object collids with the 2d boxcollider with that tag.
But my clones do not inherit the tag name "obstac", now I adjusted the script to have the clone taged with the name nbut the child obkjects in it are still not assigned so I think that is why none of tyhe clones get destroyed when colliding with the 2D boxcollider.
// repeat pipes, clone them from the prefab
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pipeRepeatScript : MonoBehaviour {
public GameObject pipes;
void Start () {
InvokeRepeating("CreateObstacle", 1f, 1.5f);
pipes.gameObject.tag = "obstac";
}
void CreateObstacle()
{
Instantiate(pipes);
}
}
--> Then here is my Destroy.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Destroyer : MonoBehaviour {
//AudioSource audio;
// void OnCollisionEnter2D(Collision2D col){
// if (col.gameObject.tag == "pipe") {
// Destroy (col.gameObject);
// Play a sound if the colliding objects had an impact.
// if (col.relativeVelocity.magnitude > 0)
// audio.Play();
// }
// }
void OnCollisionEnter(Collision col){
if(col.gameObject.tag == "pipes"){
Destroy(col .gameObject);
}
}
}
screen shots of Hierarchy - you can see the clone gets the tag name "obstac" but the children pipe1 and pipe2 do not get assigned and nothing happens when they collide with the 2D colliderbox
Your answer
![](https://koobas.hobune.stream/wayback/20220612101551im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Cannot remove sprite using destroy 1 Answer
Destroy function not documented? 2 Answers
Unity createNew Game Object after calling a Destroy() 1 Answer
destroying cloned prefab not working as intended 0 Answers
How to destroy an object gradually? 1 Answer