Skip to content

Instantly share code, notes, and snippets.

@OwenTheProgrammer
Created August 28, 2026 04:14
Show Gist options
  • Select an option

  • Save OwenTheProgrammer/7aba2426286d628b8378a49d1bc755c9 to your computer and use it in GitHub Desktop.

Select an option

Save OwenTheProgrammer/7aba2426286d628b8378a49d1bc755c9 to your computer and use it in GitHub Desktop.
Unity Reparent To Selection
using System.Linq;
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class ReparentToSelected
{
private static GameObject[] _lastSelection;
private static GameObject[] _secondLastSelection;
static ReparentToSelected()
{
Selection.selectionChanged += OnSelectionChanged;
}
private static void OnSelectionChanged()
{
_secondLastSelection = _lastSelection;
_lastSelection = Selection.gameObjects;
}
[MenuItem("GameObject/Reparent To Selected", false, 0)]
public static void InvokeAction()
{
GameObject parent = Selection.activeGameObject;
Undo.SetCurrentGroupName("Reparent to Selected");
int group = Undo.GetCurrentGroup();
{
foreach (GameObject child in _secondLastSelection)
{
Undo.SetTransformParent(
child.transform,
parent.transform,
"Reparent to Selected"
);
}
}
Undo.CollapseUndoOperations(group);
Selection.objects = _secondLastSelection.Cast<Object>().ToArray();
}
[MenuItem("GameObject/Reparent To Selected", true)]
public static bool ValidateInvokeAction()
{
if (_secondLastSelection == null)
return false;
if(_lastSelection == null || _lastSelection.Length != 1)
return false;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment