How to destroy parent object when child object is touched

Hi All,

I am created prefab with 4 child object.I am instantiated dynamically.On mouse click event of any child object i want to remove the whole object…

If am using Destroy(hit.collider.gameObject) code only particular child object is removing…

Thanks

Destroy (gameObject);

that should wipe it off the face of the planet for you :slight_smile:

Thanks for reply.

if am using Destroy(gameObject) it removes all prefab instantiated…

am creating prefab object dynamically.

I want to remove on touch of particular prefab…not all

Thanks.

You’d actually be looking to use

Destroy(hit.transform.parent.gameObject)
2 Likes

Thanks…

That code is working fine… :slight_smile:

Another way to do that is going directly to the root, because parent it’s relative to a child and… what happens if you someday change the order of your nested prefab inside of the hierachy?.. maybe the gameObject father turn into gameObject grandfather!

So, you could use instead:

Destroy(hit.transform.[b]root[/b].gameObject)

:smile:

Ooh, handy. Didn’t know there was a root property. :slight_smile:

Yes, exist and works :stuck_out_tongue:
But i can’t remember if it is a transform’s property or gameObject’s property… well, that is a minor issue after all.

Hi All,

i created prefab with one parent object and 7 child objects…My requirement is i have to remove whole particular object when user touched any child object and i have to replace one object to that hit point…

am using Destroy(hit.transform.parent.gameObject) code…it removes object
when object is touched…

my pbm is mouse drag or multiple time touched same object in different child, that function is calling that many times…

i donk know what was the pbm…Fist touch itself am removing object…but how it takes 2 touch to the same object???..

hahahahah
work for me and was funny