From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Better category name)
Line 49: Line 49:
[[Category:Editor Scripts]]
[[Category:C Sharp]]
[[Category:C Sharp]]

Revision as of 22:50, 16 August 2006

Author: Neil Carter (User:NCarter)


This script is an alternative to the built-in Make Parent command. Whereas the Make Parent command makes the first object you select the parent of subsequently selected object, this script inserts a new GameObject which becomes the parent of all selected objects.


You must place the script in a folder named Editor in your project's Assets folder for it to work properly.

Select some objects in the Scene view or Hierarchy window, then choose GameObject→Insert Parent from the menu (or press control P). Depending on the nature of the selected objects, it will have one of the following effects:

  • If only one object is selected:
    • ...and it has no parent, a new GameObject will be added, and the selected object will become a child of it.
    • ...and it already has a parent, a new GameObject will be inserted between the object and its original parent.
  • If more than one object is selected, irrespective of whether those objects already have parents, all those objects will become children of the newly created GameObject.

The newly created parent object is named _Parent to make it sort towards the top of the Hierarchy list.

C# - InsertParent.cs

<csharp>using UnityEngine; using UnityEditor; using System.Collections;

public class InsertParent : ScriptableObject {

   [MenuItem ("GameObject/Insert Parent ^p")]
   static void MenuInsertParent()
       Transform[] transforms = Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable);
       GameObject newParent = new GameObject("_Parent");
       Transform newParentTransform = newParent.transform;
       if(transforms.Length == 1)
           Transform originalParent = transforms[0].parent;
           transforms[0].parent = newParentTransform;
               newParentTransform.parent = originalParent;
           foreach(Transform transform in transforms)
               transform.parent = newParentTransform;


Personal tools