Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
9
Question by oukie · May 18, 2011 at 10:17 PM · hierarchygroup

Grouping objects in the Hierarchy?

Heya, I am currently decorating my scene with various objects in Unity, but the Hierarchy is getting extremely cluttered, is there a way to create folders there to put environment objects in to keep it all tidy? I am making a TDG so when the game is played the towers that get created clutter up the Hierarchy as well. So if there is any way to make a group, folder or anything that will not spam the Hierarchy, so do not have to scroll for 2 minutes to find 1 particular object :)

Any help is appreciated thank you.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oukie · May 19, 2011 at 04:10 PM 0
Share

Ah yes thank you, works like a charm :) I would give you the thumbs up but it says im not allowed.

avatar image TonyLi · Sep 27, 2013 at 12:44 PM 0
Share

You should still be able to mark oliver-jones's answer as Accepted (the checkbox under the thumbs), which will help others find this answer if they have the same question.

BTW, from the Transform reference: "When parenting Transforms, set the parent's location to <0,0,0> before adding the child. This will save you many headaches later. "

avatar image Jamora · Sep 27, 2013 at 12:50 PM 0
Share

There is also a search bar in the hierarchy window. If you know what you're searching for, you never have to scroll.

7 Replies

· Add your reply
  • Sort: 
avatar image
38

Answer by bjennings76 · Jul 20, 2015 at 11:52 PM

Here's a little Unity editor script I use to group selected objects. Just save this as Assets/Editor/GroupCommand.cs. Use 'Ctrl-G' or 'GameObject Menu > Group Selected'. It also supports Undo:

 using UnityEditor;
 using UnityEngine;
 
 public static class GroupCommand
 {
     [MenuItem("GameObject/Group Selected %g")]
     private static void GroupSelected()
     {
         if (!Selection.activeTransform) return;
         var go = new GameObject(Selection.activeTransform.name + " Group");
         Undo.RegisterCreatedObjectUndo(go, "Group Selected");
         go.transform.SetParent(Selection.activeTransform.parent, false);
         foreach (var transform in Selection.transforms) Undo.SetTransformParent(transform, go.transform, "Group Selected");
         Selection.activeGameObject = go;
     }
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jeromeWork · Jul 05, 2018 at 09:22 AM 0
Share

Thank you for the share. Can't believe this isn't part of the Editor by default!

For info, if you use an asset like Gaia that has Cntrl G already reserved, you might want to change the keyboard shortcut. That's the '%g' part of the $$anonymous$$enuItem.

avatar image guitarjorge24 · May 07, 2019 at 01:16 AM 0
Share

Is there a way to change this script so that the objects are inserted into the group in the same order they were outside the group? Like if I have Enemy (1), Enemy (2), Enemy (3) in order I would like them to stay in order after they are moved inside the group.

avatar image amit-chai · Jan 29, 2021 at 09:47 AM 0
Share

O$$anonymous$$G, I can't believe I lived without this genius script. thanks!!!!!

avatar image aliemci · Feb 20, 2021 at 11:21 PM 1
Share

Ungroup Script (No Undo) CTRL + G = Group CTRL + SHIFT + G = Ungroup

     [MenuItem("GameObject/Ungroup Selected #%g")]
     private static void UngroupSelected()
     {
         if (!Selection.activeTransform) 
             return;
 
         int childCount = Selection.transforms[0].childCount;
 
         // Only first element
         for (int j = 0; j < childCount; j++)
         {
             Transform child = Selection.transforms[0].GetChild(0);
 
             child.SetParent(null);
             child.SetSiblingIndex(Selection.transforms[0].GetSiblingIndex());
         }
 
         GameObject.DestroyImmediate(Selection.transforms[0].gameObject);
     }
avatar image ygokayo_unity · May 02, 2021 at 03:24 AM 0
Share

Added a tiny line, the new group will stay in place know instead of going to the bottom of hierarchy.

 using UnityEditor;
 using UnityEngine;
 
 public static class GroupCommand
 {
     [MenuItem("GameObject/Group Selected %g")]
     private static void GroupSelected()
     {
         if (!Selection.activeTransform) return;
         var go = new GameObject(Selection.activeTransform.name + " Group");
         Undo.RegisterCreatedObjectUndo(go, "Group Selected");
         go.transform.SetParent(Selection.activeTransform.parent, false);
         go.transform.SetSiblingIndex(Selection.activeTransform.GetSiblingIndex());
         foreach (var transform in Selection.transforms)
         {
             Undo.SetTransformParent(transform, go.transform, "Group Selected");
         }
         Selection.activeGameObject = go;
     }
 }

avatar image
8

Answer by oliver-jones · May 19, 2011 at 02:05 AM

Yes there is, but they aren't really folders. If you go to GameObject > Create Empty this will place an empty object on your scene as well as your Hierarchy, you can then drag particular objects into that without affecting your game at all.

Example:

You have a butt load of lights in your scene. You can then create a Game Empty and call it 'Lights' and then drag in all your lights into the object.

Think of it more like a 'null' object.

Hope that helps.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Andy2222 · Sep 27, 2013 at 11:06 AM 1
Share

Actually a "empty" gameobject still has a transform (cant be removed), which means that any normal logic that works via "transform.root" will break. Given the complex child/component relation unity uses, i would not advice to use this "hack" to simulate folders/grouping.

Often its to complicated to sort out what child may or may not have a certain component, so its common practice to search from the transform.root, which in the case of those "folder" objects will result in wrong or unpredictable behavior.

avatar image Jamora Andy2222 · Jan 06, 2014 at 11:39 AM 2
Share

You can work around this by creating an extension method for Transform; one that recursively moves upwards the hierarchy and returns the actual root transform (the one below the folder-transform). This can be achieved by

 public static Transform root (this Transform tform, bool ignoreFolderTransform){
     
     if(tform.parent == null)
         return null;
     Transform temp = tform.parent.root(true);
     if( temp == null)
         temp = tform;
     return temp;
 }

Now, the only thing that the developer needs to remember is to add a bool (doesn't really matter if it's true or false with this implementation) to go with the root-call.

avatar image Ziplock9000 · Oct 25, 2018 at 09:47 AM 1
Share

This is a bad answer as a GameObject is far more than an organisational tool.

avatar image
2

Answer by Lyrcaxis · May 02, 2018 at 04:51 PM

What I do is group them in an empty Gameobject, which has a script that deletes itself on runtime.

 using UnityEngine;
 public class UnrootChildrenAndDeleteOnRunTime : MonoBehaviour {
     void Awake() {
         while (transform.childCount > 0) {
             transform.GetChild(0).parent = null;
         }
         Destroy(this.gameObject);
     }
 }

Add this to the group's root and you're good to go!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lyrcaxis · May 02, 2018 at 04:55 PM 0
Share

It's still a pain to find stuff on runtime though.. so I came here searching for a new way :D

avatar image
1
Wiki

Answer by jorjdboss · Jan 06, 2014 at 11:04 AM

Create an empty gameobject and reset it's position to (0,0,0). Drag this the project folder to create a prefab. Whenever you need to group objects, drag this prefab into the scene and it's position will remain at the origin. Just make sure not to press apply on any of the groups.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Cayca · Jan 06, 2014 at 10:56 AM

I read that this could be prevented by setting the 0-Object (used as folder) to 0 0 0.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hierarchy question 1 Answer

Text Labels For Prefabs in Hierarchy Turn Pink 2 Answers

Scene corrupted - enpty hierarchy 0 Answers

Parse Scene Hierarchy 1 Answer

Force select object in hierarchy via editor script? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges