- Home /
Parent everything to gameobject
Hey,
I want everything that is created in the hierarchy to be automatically parented to a gameobject, is this possible without continuously checking every gameobject's parent/root in the scene?
Answer by Khena_B · Feb 04, 2018 at 11:55 PM
Solved it by using EditorApplication.hierarchyWindowChanged to check only once if something has changed in the hierarchy
[InitializeOnLoad]
class HierarchyClass
{
static HierarchyClass()
{
EditorApplication.hierarchyWindowChanged += HierarchyChangedCallback;
}
static void HierarchyChangedCallback()
{
//Do something once, when the hierarchy has changed
}
Answer by ElijahShadbolt · Feb 04, 2018 at 11:50 PM
If you are instantiating prefabs at runtime: When it is being instantiated use something like
newInstance.transform.SetParent(parentTransform);
or
newInstance = (GameObject)Instantiate(prefab.gameObject, parentTransform);
If you are in the editor, just right click the parent in the hierarchy when creating new objects. (don't use the GameObject menu in the ribbon)
For a more secure (but slow) solution in the editor, you could create an EditorWindow script which uses EditorWindow.Update to check every object each frame. This won't affect builds, only the editor.
I was trying to parent stuff that is created with the "create" menu and not when instantiated by script, but thanks!
Oh that is brilliant! Use @$$anonymous$$henaB 's solution: EditorApplication.hierarchyWindowChanged