Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Destroy the Parent of the Parent (of the child)

Discussion in 'Scripting' started by josemauriciob, Oct 21, 2015.

  1. josemauriciob

    josemauriciob

    Joined:
    Mar 5, 2009
    Posts:
    662
    hi ..
    just to know how to destroy the parent of the parent of the child

    the child has the script
    and the rigid body, then when get full damage, i need to destroy
    entire hierarchy, beginning above two parents.

    is there another way to do...
    i just know how to destroy one parent with this line (script)...
    .
    Destroy(transform.parent.gameObject);

    any help will be apreciate !
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Code (csharp):
    1.  
    2. Destroy(transform.parent.parent.gameObject);
    3.  
    'parent' is a reference to the parent Transform, therefore it too has a parent property.

    Of course, I personally would avoid a design that required static hierarchies in its structure. I usually have some script that I put on the 'root' GameObject that represents that collection of GameObjects (which I call an 'Entity'). You can then dig up the hierarchy chain to get that script. This can easily be done with the GetComponentInParent method:

    http://docs.unity3d.com/ScriptReference/Component.GetComponentInParent.html

    Yes, that method searches continuously up the parent chain until it reaches the root of the scene.

    The script I usually use to represent entities is this one:
    https://github.com/lordofduct/spacepuppy-unity-framework/blob/master/SpacepuppyBase/SPEntity.cs
    But it doesn't have to be as complex as that one. It could just be a script called 'Entity' and have no real code in it.

    Like so:
    Code (csharp):
    1.  
    2. public class Entity : MonoBehaviour
    3. {
    4.  
    5. }
    6.  
    7. //elsewhere for destroy
    8. Destroy(this.gameObject.GetComponentInParent<Entity>().gameObject);
    9.  

    Another method is I have a 'tag' titled 'Root', and search up the parent chain for the GameObject with that tag.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Destroying your parents often leads to unintended consequences. I tend to use ExecuteEvents.ExecuteHierachy. The parent object can then choose to ignore or process the event as appropriate.
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    transform.root will give you the top parent. Not 100% sure if thats what youre after.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    It does make things messy later when you decide to group your hierarchy.
     
  6. josemauriciob

    josemauriciob

    Joined:
    Mar 5, 2009
    Posts:
    662
    thanks a lot... but using tags... the problem is, i have a lot enemies instantiate, and will destroy all with this tags
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    yep wont work if you try clean up your hierarchy via parenting.
     
  8. josemauriciob

    josemauriciob

    Joined:
    Mar 5, 2009
    Posts:
    662
    in conclusion , what is the best way to do this please...
    Sorry for insisting

    thanks so much
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I mainly mention it as a caution. I once inherited a zombie survival project. Each wall in every house was a destructible object. Each house was part of a village.

    The logical structure would have been to parent all the walls to a house, and parent all the houses to the village.

    But the destructible object system used Destroy(transform.root.gameObject). Every destructible object in the scene was it's own parent. Which made level design a nightmare. I quickly got sick of moving a house and leaving one wall behind, and went back and removed every line that referred to transform.root.

    Needless to say transform.root has made it into my 'not suitable for production' list. I now use various versions of climbing the hierarchy to find something willing to deal with the event.
     
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    transform.parent.parent is the answer to the question you asked. I'm not sure how tags came into this question at all, but tags are unrelated to either the question asked or the answer given.
     
    Kiwasi likes this.
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Yup. Once someone posts a correct answer we have a general habit of navel gazing. Transform.parent.parent will work for simple games and prototypes. In some cases the fastest solution to code is the best answer.
     
  12. josemauriciob

    josemauriciob

    Joined:
    Mar 5, 2009
    Posts:
    662
    not working ...... using
    Code (JavaScript):
    1. Destroy(transform.root.gameObject);
     
  13. josemauriciob

    josemauriciob

    Joined:
    Mar 5, 2009
    Posts:
    662
    but i already have more than 15 identical prebas clons, with the same tag.....
    and i need to destroy 5 parents ......
    and i need to destroy all the gameobject hirarchy.
    and this line is not working....

    Code (csharp):
    1. Destroy(transform.parent.parent.parent.parent.parent.gameObject);
     
  14. josemauriciob

    josemauriciob

    Joined:
    Mar 5, 2009
    Posts:
    662
    [ SOLVED ]
    i put here if any need it...
    OnTriggerEnter ( other ....

    Code (JavaScript):
    1. Destroy(other.transform.root.gameObject);    
     
  15. SolenopsisCampo

    SolenopsisCampo

    Joined:
    Jun 1, 2020
    Posts:
    6
    Code (CSharp):
    1.     private void OnCollisionEnter(Collision collision)
    2.     {
    3.         Destroy(gameObject);
    4.         if (collision.gameObject.layer == LayerMask.NameToLayer("Enemy"))
    5.         {
    6.             Destroy(collision.gameObject.transform.root.gameObject);
    7.         }
    8.     }
    9.  
    this also works for me