- Home /
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.
Ah yes thank you, works like a charm :) I would give you the thumbs up but it says im not allowed.
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. "
There is also a search bar in the hierarchy window. If you know what you're searching for, you never have to scroll.
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;
}
}
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.
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.
O$$anonymous$$G, I can't believe I lived without this genius script. thanks!!!!!
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);
}
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;
}
}
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.
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.
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.
This is a bad answer as a GameObject is far more than an organisational tool.
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!
It's still a pain to find stuff on runtime though.. so I came here searching for a new way :D
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.
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.
Your answer
Follow this Question
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