- Home /
How can I destroy autogenerated child gameobject?,Help with collision object destroy
Hello folks,
Merry Christmas and I wish you all Happy New Year 2018!!
Ok so my problem is what C# script could I use to attach the the KFC gameobject to detect and destoy the autogenerated building? It has no name and as you can see on the picture its a child of a child of a child. It has a auto mesh collider to it.
Maybe its a realy simple line of code I need to use but I tried many and had no luck at all for 2 days :( NOTE: I have added a box collider to the KFC gameobject also just in case.
If anyone can hep me with this please.
Best.,
Answer by nt314p · Dec 23, 2017 at 08:17 PM
Use OnCollisionEnter.
More can be found here: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter.html
Your code might look something like (EDITED)
void OnCollisionEnter (Collision col) {
if(col.gameObject.tag == "building"){
Destroy(col.gameObject);
}
}
If its possible to tag the building gameobjects with the tag building, this code should work.
Answer by ttbgamesllc · Dec 24, 2017 at 12:02 PM
@n314p thanks for your quick reply and help but i tried OnCollisionEnter, OnCollisiinExit, OnCollisionStay ect. ect. and didnt had any luck. The building mesh are auto generated so they doesnt have same name and also if you pay attention they doesnt have name... The parent gameobject have a name.
What about if I create a layermask what could I use to attach to the KFC so when it collides with the building it destroys it?
Hi! How's it going? You putted the image two times.
From what you say, you only missing a way to identify the auto-generated buildings. So, you know, tag it, name it, or whatever. Then something like what n314p suggested should work.
If you are using prefabs, tag the prefab, if not, tag the auto-generated building on a script.