Search Unity

How to create new GameObject as child in hierarchy

Discussion in 'Editor & General Support' started by goodhustle, Feb 10, 2011.

  1. goodhustle

    goodhustle

    Joined:
    Jun 4, 2009
    Posts:
    310
    Maybe I missed this in the manual somewhere... but I can't figure out if there's a way to get the instantiations from the Editor's GameObject panel to respect my current place in the hierarchy. It always goes to the root, so it ends up just being placed alphabetically, and with a large scene, it's annoying to have to scroll around to add new GameObjects. I know about duplication, but it would be a nice convenience if it would place new GameObjects as a child of the currently selected GO in the hierarchy. Is there any way to do this?
     
    corvin666 likes this.
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  3. goodhustle

    goodhustle

    Joined:
    Jun 4, 2009
    Posts:
    310
    klesun and walid-dev like this.
  4. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    I don't think Unity offers the feature you describe, but you could always request it in the 'feature requests' section of the site.
     
    renanribas2003 likes this.
  5. chomp

    chomp

    Joined:
    Dec 6, 2010
    Posts:
    22
    I would like to second this.

    Every time I create a GameObject from the application menu, it is added to the root of the scene.

    An intuitive default behavior would be to have the GameObject creation menu items use the current selection as the new object's parent. There is nothing intuitive about dumping everything into the scene root by default, and I'm seriously baffled that this is the default behavior. As a scene hierarchy grows in complexity, this existing behavior becomes increasingly more inconvenient.
     
    klesun likes this.
  6. chomp

    chomp

    Joined:
    Dec 6, 2010
    Posts:
    22
    As an addendum to my mad ranting, I have been inspired to solve the problem myself. Here is a simple editor script (drop it in Assets/Editor/FixStupidEditorBehavior.cs) to alleviate GameObject creation woes.

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class FixStupidEditorBehavior : MonoBehaviour {
    5.     [MenuItem("GameObject/Create Empty Child #&n")]
    6.     static void BringMeBackFromTheEdgeOfMadness() {
    7.         GameObject go = new GameObject("GameObject");
    8.         if(Selection.activeTransform != null)
    9.             go.transform.parent = Selection.activeTransform;
    10.     }
    11. }
    12.  
    This adds a new "GameObject/Create Empty Child" menu item (Shift+Alt/Opt+N shortcut) which creates an empty GameObject as a child of the currently active scene object, if any. If no in-scene object is selected, the new GameObject will be left in the scene root.
     
  7. DarrinLile

    DarrinLile

    Joined:
    Dec 11, 2010
    Posts:
    12
    Hmm, I'll have to try this. Thanks!
     
    Boegi likes this.
  8. goodhustle

    goodhustle

    Joined:
    Jun 4, 2009
    Posts:
    310
    Wow, thanks Pixels!
     
  9. buffonomics

    buffonomics

    Joined:
    Jun 10, 2009
    Posts:
    59
    Thanks pixels.
    Here are 3 modifications to Pixel's Empty Game Object code if anyone needs it.

    1) Spawn as duplicate (i.e same parent) as selected.
    2) Spawn as parent of selected.
    3) Will always spawn at the exact same location of the selected (like if you were going to duplicate).

    Feel free to change the assigned shortcuts to suit your workflow.
    e.g "#&e" means Shift+alt+e

    Code (csharp):
    1.  
    2.     using UnityEngine;
    3.  
    4.     using UnityEditor;
    5.  
    6.      
    7.  
    8.     public class FixStupidEditorBehavior : MonoBehaviour {
    9.  
    10.        
    11.  
    12.         [MenuItem("GameObject/Create Empty Child #&e")]
    13.  
    14.         static void createEmptyParent() {
    15.  
    16.             GameObject go = new GameObject("GameObject");
    17.  
    18.             if(Selection.activeTransform != null)
    19.  
    20.             {
    21.  
    22.                     go.transform.parent = Selection.activeTransform.parent;
    23.  
    24.                     go.transform.Translate(Selection.activeTransform.position);
    25.  
    26.                     Selection.activeTransform.parent = go.transform;
    27.  
    28.             }
    29.  
    30.         }  
    31.  
    32.    
    33.  
    34.         [MenuItem("GameObject/Create Empty Duplicate #&d")]
    35.  
    36.         static void createEmptyDuplicate() {
    37.  
    38.             GameObject go = new GameObject("GameObject");
    39.  
    40.             if(Selection.activeTransform != null)
    41.  
    42.             {
    43.  
    44.                     go.transform.parent = Selection.activeTransform.parent;
    45.  
    46.                     go.transform.Translate(Selection.activeTransform.position);
    47.  
    48.             }
    49.  
    50.         }
    51.  
    52.    
    53.  
    54.         [MenuItem("GameObject/Create Empty Child #&c")]
    55.  
    56.         static void createEmptyChild() {
    57.  
    58.             GameObject go = new GameObject("GameObject");
    59.  
    60.             if(Selection.activeTransform != null)
    61.  
    62.             {
    63.  
    64.                     go.transform.parent = Selection.activeTransform;
    65.  
    66.                     go.transform.Translate(Selection.activeTransform.position);
    67.  
    68.             }
    69.  
    70.         }
    71.  
    72.     }
    73.  
     
    Last edited: Feb 4, 2012
  10. Nodrap

    Nodrap

    Joined:
    Nov 4, 2011
    Posts:
    83
    Very good - is so good that Unity lets you compensate for its own limitations!

    P.S. There is a typo on line 11 - should read [MenuItem("GameObject/Create Empty Parent #&e")]
     
  11. tylo

    tylo

    Joined:
    Dec 7, 2009
    Posts:
    154
    Fixed a small typo on Line 7, which should read "Create Empty Parent".

    Also, I fixed the formatting, I hope. Nope, the forums just seem to cause double-whitespace formatting problems.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4.     using UnityEditor;    
    5.  
    6.     public class FixStupidEditorBehavior : MonoBehaviour {
    7.  
    8.         [MenuItem("GameObject/Create Empty Parent #&e")]
    9.         static void createEmptyParent() {
    10.             GameObject go = new GameObject("GameObject");
    11.             if(Selection.activeTransform != null)
    12.             {
    13.  
    14.                     go.transform.parent = Selection.activeTransform.parent;
    15.  
    16.                     go.transform.Translate(Selection.activeTransform.position);
    17.  
    18.                     Selection.activeTransform.parent = go.transform;
    19.  
    20.             }
    21.  
    22.         }  
    23.  
    24.  
    25.         [MenuItem("GameObject/Create Empty Duplicate #&d")]
    26.         static void createEmptyDuplicate() {
    27.  
    28.             GameObject go = new GameObject("GameObject");
    29.  
    30.             if(Selection.activeTransform != null)
    31.             {
    32.                 go.transform.parent = Selection.activeTransform.parent;
    33.                 go.transform.Translate(Selection.activeTransform.position);
    34.             }
    35.  
    36.         }
    37.  
    38.         [MenuItem("GameObject/Create Empty Child #&c")]
    39.         static void createEmptyChild() {
    40.  
    41.             GameObject go = new GameObject("GameObject");
    42.  
    43.             if(Selection.activeTransform != null)
    44.             {
    45.                     go.transform.parent = Selection.activeTransform;
    46.                     go.transform.Translate(Selection.activeTransform.position);
    47.             }
    48.  
    49.         }
    50.  
    51.     }
     
    dalekvan likes this.
  12. rbisso

    rbisso

    Joined:
    Jul 13, 2011
    Posts:
    29
    You guys are awesome. I need to get into the habit of immediately checking the forums when I run into an annoyance like this... would have saved me much time and chagrin over time!
     
  13. HapaFive

    HapaFive

    Joined:
    Jan 15, 2013
    Posts:
    7
    This has been driving me crazy. I love you people!
     
  14. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
  15. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,963
    Awesome!
     
  16. tigertrussell

    tigertrussell

    Joined:
    Mar 29, 2013
    Posts:
    22
    Here is my take on this script.

    You can use Shift + Alt + C to create a new child and select that new child.
    You can use Shift + Alt + W to "wrap" many selected items underneath a single new parent.

    Thanks to those who wrote the scripts before this one! I love this!

    Code (csharp):
    1.     using UnityEngine;
    2.     using UnityEditor;
    3.  
    4.     public class MinorFixes : MonoBehaviour {
    5.  
    6.         [MenuItem("GameObject/Add Empty Child #&c")]
    7.         static void AddEmptyChild() {
    8.             GameObject go = new GameObject("EmptyChild:NameMe");
    9.             if(Selection.activeTransform != null)
    10.             {
    11.                 go.transform.parent = Selection.activeTransform;
    12.                 go.transform.Translate(Selection.activeTransform.position);
    13.             }
    14.             Selection.activeTransform = go.transform;
    15.         }
    16.  
    17.         [MenuItem("GameObject/Wrap in Object #&w")]
    18.         static void WrapInObject() {
    19.             if(Selection.gameObjects.Length == 0)
    20.                 return;
    21.  
    22.             GameObject go = new GameObject("Wrapper:NameMe");
    23.             go.transform.parent = Selection.activeTransform.parent;
    24.             go.transform.position = Vector3.zero;
    25.             foreach(GameObject g in Selection.gameObjects) {
    26.                 g.transform.parent = go.transform;
    27.             }
    28.         }
    29.     }
     
    Last edited: Jun 14, 2013
  17. betaalpha

    betaalpha

    Joined:
    Apr 28, 2013
    Posts:
    6
    Quick note for anyone using the NGUI plugin - they've already done this. Just hit SHIFT+ALT+N to create a child in the selected game object.
     
  18. ArtiomFedulov

    ArtiomFedulov

    Joined:
    Apr 20, 2014
    Posts:
    6
    Hi guys, I know this is an old thread, but you can easely just drug and drop one object to other in object list.
     
  19. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    If you're going to bring a 3-year-old thread up from the dregs, could you at least read it first?
     
  20. almo

    almo

    Joined:
    Jul 14, 2010
    Posts:
    83
    So I copied your code here because it's pretty cool, and when I paste, it... like... puts the line numbers in!

    Am I doing something wrong, or is the forum software that dorky?
     
  21. WilliamLv

    WilliamLv

    Joined:
    Feb 27, 2014
    Posts:
    4
    Great, Mark
     
  22. crafTDev

    crafTDev

    Joined:
    Nov 5, 2008
    Posts:
    1,820
    Hello,

    Any update to allow this to paste gameobjects that were copied to be children of the selected object?

    Thanks,
    jrDev
     
    Magic73 likes this.