How to create new GameObject as child in hierarchy

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?

1 Like

Check this out: http://unity3d.com/support/documentation/ScriptReference/Transform-parent.html

1 Like

Nope, this isn’t what I’m talking about. Changing parent relationships via script is easy. I was curious if the Editor could place GameObjects as a child to the currently selected GameObject when you add things through the GameObject menu in the taskbar.

2 Likes

I don’t think Unity offers the feature you describe, but you could always request it in the ‘feature requests’ section of the site.

1 Like

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.

1 Like

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.

using UnityEngine;
using UnityEditor;

public class FixStupidEditorBehavior : MonoBehaviour {
	[MenuItem("GameObject/Create Empty Child #&n")]
	static void BringMeBackFromTheEdgeOfMadness() {
		GameObject go = new GameObject("GameObject");
		if(Selection.activeTransform != null)
			go.transform.parent = Selection.activeTransform;
	}
}

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.

10 Likes

Hmm, I’ll have to try this. Thanks!

1 Like

Wow, thanks Pixels!

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

    using UnityEngine;

    using UnityEditor;

     

    public class FixStupidEditorBehavior : MonoBehaviour {

        

		[MenuItem("GameObject/Create Empty Child #&e")]

        static void createEmptyParent() {

            GameObject go = new GameObject("GameObject");

            if(Selection.activeTransform != null)

			{

	                go.transform.parent = Selection.activeTransform.parent;

					go.transform.Translate(Selection.activeTransform.position);

					Selection.activeTransform.parent = go.transform;

			}

        }	

	

		[MenuItem("GameObject/Create Empty Duplicate #&d")]

        static void createEmptyDuplicate() {

            GameObject go = new GameObject("GameObject");

            if(Selection.activeTransform != null)

			{

	                go.transform.parent = Selection.activeTransform.parent;

					go.transform.Translate(Selection.activeTransform.position);

			}

        }

	

		[MenuItem("GameObject/Create Empty Child #&c")]

        static void createEmptyChild() {

            GameObject go = new GameObject("GameObject");

            if(Selection.activeTransform != null)

			{

	                go.transform.parent = Selection.activeTransform;

					go.transform.Translate(Selection.activeTransform.position);

			}

        }

    }

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”)]

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.

using UnityEngine;

    using UnityEditor;     

    public class FixStupidEditorBehavior : MonoBehaviour {

        [MenuItem("GameObject/Create Empty Parent #&e")]
        static void createEmptyParent() {
            GameObject go = new GameObject("GameObject");
            if(Selection.activeTransform != null)
            {

                    go.transform.parent = Selection.activeTransform.parent;

                    go.transform.Translate(Selection.activeTransform.position);

                    Selection.activeTransform.parent = go.transform;

            }

        }   


        [MenuItem("GameObject/Create Empty Duplicate #&d")]
        static void createEmptyDuplicate() {

            GameObject go = new GameObject("GameObject");

            if(Selection.activeTransform != null)
            {
			    go.transform.parent = Selection.activeTransform.parent;
                go.transform.Translate(Selection.activeTransform.position);
			}

        }

        [MenuItem("GameObject/Create Empty Child #&c")]
        static void createEmptyChild() {

            GameObject go = new GameObject("GameObject");

            if(Selection.activeTransform != null)
            {
                    go.transform.parent = Selection.activeTransform;
                    go.transform.Translate(Selection.activeTransform.position);
            }

        }

    }
1 Like

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!

This has been driving me crazy. I love you people!

top

Awesome!

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!

    using UnityEngine;
    using UnityEditor;

    public class MinorFixes : MonoBehaviour {

        [MenuItem("GameObject/Add Empty Child #&c")]
        static void AddEmptyChild() {
            GameObject go = new GameObject("EmptyChild:NameMe");
            if(Selection.activeTransform != null)
            {
                go.transform.parent = Selection.activeTransform;
                go.transform.Translate(Selection.activeTransform.position);
            }
            Selection.activeTransform = go.transform;
        } 

        [MenuItem("GameObject/Wrap in Object #&w")]
        static void WrapInObject() {
            if(Selection.gameObjects.Length == 0)
                return;

            GameObject go = new GameObject("Wrapper:NameMe");
            go.transform.parent = Selection.activeTransform.parent;
            go.transform.position = Vector3.zero;
            foreach(GameObject g in Selection.gameObjects) {
                g.transform.parent = go.transform;
            }
        }
    }

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.

Hi guys, I know this is an old thread, but you can easely just drug and drop one object to other in object list.

If you’re going to bring a 3-year-old thread up from the dregs, could you at least read it first?

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?