- Home /
How do I create a new object in the Editor as a child of another object?
Currently, if I select an object in the Hierarchy pane and then select Game Object->Create Other or Create Empty with the intention of attaching the new object as a child of the selected one, the new object ends up unparented at the bottom of the hierarchy and I have to drag it back onto the intended parent, and then set the translation of the child back to zero. Is there a way to create an object in the Editor so that it immediately is attached as a child of another object?
I've never understood why "Create X" doesn't create X as a child of the currently selected game object.
Answer by runevision · Nov 12, 2009 at 10:20 AM
There is no functionality for this build in in Unity, but for the case of empty GameObjects, you can use an editor script that provides the functionality.
Place this javascript in a folder called "editor", somewhere in the Assets folder:
// Add menu to the main menu @MenuItem ("GameObject/Create Empty As Child") static function CreateGameObjectAsChild () { var go : GameObject = new GameObject ("GameObject"); go.transform.parent = Selection.activeTransform; go.transform.localPosition = Vector3.zero; }
// The item will be disabled if no transform is selected. @MenuItem ("GameObject/Create Empty As Child", true) static function ValidateCreateGameObjectAsChild () { return Selection.activeTransform != null; }
// Add context menu to Transform's context menu @MenuItem ("CONTEXT/Transform/Create Empty As Child") static function CreateGameObjectAsChild (command:MenuCommand) { var tr : Transform = command.context; var go : GameObject = new GameObject ("GameObject"); go.transform.parent = tr; go.transform.localPosition = Vector3.zero; }
Your answer
Follow this Question
Related Questions
Finding a GameObject in an editor script based on position 2 Answers
DestroyImmediate(component.gameObject) destroys component but not gameObject 1 Answer
How to access Custom Inspector own gameObject 1 Answer
GameObject HideFlags.DontSave not working as expected 1 Answer
Editor Scripting Question 1 Answer