Search Unity

moving child objects in hierarchy and keep stacks/sorting? Help needed

Discussion in 'Scripting' started by RamiT, Aug 30, 2015.

  1. RamiT

    RamiT

    Joined:
    Jan 7, 2014
    Posts:
    5
    Hello, so I really need help with this one. How I move all child objects from "fem1" to "character" and keep stacks/sorting..

    my script does it like this(3rd pic) and 2 pic is how it should be.. :(

    https://gyazo.com/13234bfcb44fb06f46ebcdda1fb9f36e

    script:
    Code (JavaScript):
    1. #pragma strict
    2. //This script must be attached to clothing or body and functions can be used by UI buttons
    3.  
    4. var selected : boolean;
    5. private var newParent : GameObject;
    6. private var parentOBJ : Transform;
    7.  
    8.  
    9. function Start () {
    10.  
    11. newParent = GameObject.Find("character");
    12. parentOBJ = transform.parent;
    13.  
    14.  
    15. }
    16.  
    17. function Update () {
    18.  
    19. var renderers = GetComponentsInChildren(Renderer);
    20. var transforms = GetComponentsInChildren(Transform);
    21.  
    22.     if (selected)
    23.         {
    24.  
    25.             gameObject.tag = "active";
    26.  
    27.                        
    28.             for (var r : Renderer in renderers) { r.enabled = true; }
    29.  
    30.             for (var child : Transform in transforms) { child.transform.parent = newParent.transform;    }
    31.         }
    32.     else
    33.         {
    34.  
    35.             gameObject.tag = "de_active";
    36.  
    37.             for (var r : Renderer in renderers) { r.enabled = false; }
    38.             this.transform.parent = parentOBJ.transform;  
    39.            
    40.  
    41.         }
    42.  
    43.  
    44. }
    45.  
    46. function MAKE_OBJ_ACTIVE ()
    47. {
    48.      selected = !selected;
    49. }
    50.  
     
  2. Redtail87

    Redtail87

    Joined:
    Jul 17, 2013
    Posts:
    125
    You need to only change the top level gameobject's parents. You can get only the top level objects by using the transform in a using statement (which is not very intuitive to me)

    Like this: for(var child : Transform in parentOBJ.transform)
     
  3. RamiT

    RamiT

    Joined:
    Jan 7, 2014
    Posts:
    5
    Thank you for reply :) I tested that script it moved parent object including all childrens. I mean I want to keep parent object, but exctract all children objects to object in hierarchy. Problem is if move whole package like this to newparent "character" pre-setup animations won't work I think bone data and everything must be in root and in order. I'am very bad in explaining hope it makes sense.

    I think this is one way to do it :D it's pretty complex system.. but it works. Critics?

    explained somehow in picture - https://gyazo.com/bde7b709aaae7da0b00f9d1dc8ac20c7

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var item : boolean;
    4. var character : boolean;
    5. var characterExtracter : boolean;
    6.  
    7. var selected : boolean;
    8.  
    9. private var newParent : GameObject;    private var newParent2 : GameObject;
    10. private var parentOBJ : Transform;
    11.  
    12.  
    13. function Start ()
    14. {
    15.  
    16. newParent = GameObject.Find("character");    newParent2 = GameObject.Find("CHARACTER_EXTRACTER");
    17. parentOBJ = transform.parent;
    18.  
    19.  
    20. }
    21.  
    22. function Update () {
    23.  
    24. var renderers = GetComponentsInChildren(Renderer);
    25. var transforms = GetComponentsInChildren(Transform);
    26. var allChilds;
    27.  
    28.     if (selected)
    29.         {
    30.             gameObject.tag = "active";      
    31.             for (var r : Renderer in renderers) { r.enabled = true; }
    32.            
    33.             if (item)
    34.             {
    35.             this.transform.parent = newParent.transform;  
    36.                 //for (var child : Transform in transforms) { child.transform.parent = newParent.transform;    }
    37.             }
    38.  
    39.  
    40.             if (character)
    41.             {
    42.                 this.transform.parent = newParent2.transform;  
    43.             }
    44.  
    45.  
    46.             if (characterExtracter)
    47.             {
    48.                 allChilds = transform.GetChild(0);
    49.                 for (var child : Transform in allChilds) { child.gameObject.transform.parent = newParent.transform;    }
    50.             }
    51.            
    52.  
    53.         }
    54.     else
    55.         {
    56.             gameObject.tag = "de_active";
    57.             for (var r : Renderer in renderers) { r.enabled = false; }
    58.                
    59.            
    60.             if (item)
    61.             {
    62.             this.transform.parent = parentOBJ.transform;  
    63.             }
    64.         }
    65.  
    66.  
    67. }
    68.  
    69. function MAKE_OBJ_ACTIVE ()
    70. {
    71.      selected = !selected;
    72. }
    73.