- Home /
How to I force the selection of a parent when a child is selected in the editor?
Lets say I have a parent object with a child attached. Inside the editor the user selects the child object. I want to force the editor to select it's parent.
How do I do that?
Answer by Cameron Petty · Nov 03, 2015 at 05:59 AM
Very old question, but I came upon it tonight when trying to remember the answer.
The attribute may well have been added to the API after this question was asked, for all I know, but for any others that might happen by here, the functionality desired is the SelectionBaseAttribute.
[SelectionBase]
public class PlayerScript : MonoBehaviour {
}
Answer by oscarsweatman · Mar 03, 2012 at 11:08 AM
I've noticed that if any gameobject is created from a prefab (its text will appear blue in the hierarchy window) Unity will select the parent object if any of the children objects are clicked in the view.
Example: If you add two simple game objects to the scene (sphere, cube, etc) and parent one to the other, Unity will always select the one you click whether it's the parent or child.
If you take that same hierarchy of objects and make a prefab out of it and add that prefab to the scene, Unity will select the parent object (the entire prefab) in the scene.
Also, if you are needing to do this through script you can modify the current selection by using the "Selection" class:
(C#)
Selection.activeGameObject = Selection.activeGameObject.transform.parent;
You can find the Selection class documentation here.
Hope that answers your question :)
Answer by dmi3iy · Mar 09, 2019 at 04:22 PM
I wrote this little script, based on Help and code above. When you select object in editor window and after that press alt + C, you will get the selected parent of object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class SelectParent : EditorWindow {
[MenuItem("Edit/Select parent &c")]
static void SelectParentOfObject()
{
Selection.activeGameObject = Selection.activeGameObject.transform.parent.gameObject;
}
}
Great idea! Make it even better with some null checks:
Selection.activeGameObject = Selection.activeGameObject?.transform.parent?.gameObject;
I ran into this problem today when I wanted to select thousands of trees and delete them but the editor kept selecting children instead of the parents. I built upon your script to make it work for multiple selected objects.
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class SelectParent : EditorWindow
{
[MenuItem("Edit/Select Parent &c")]
static void SelectParentOfObjects()
{
List<GameObject> newSelection = new List<GameObject>();
foreach (var s in Selection.objects)
{
newSelection.Add((s as GameObject).transform.parent?.gameObject);
}
Selection.objects = newSelection.ToArray();
}
}
Your answer
Follow this Question
Related Questions
How to minimize the main editor Window by script 1 Answer
Why would editor script be slow on first compilation? 0 Answers
Help with scrollbar and offset 1 Answer
Detecting scene save - replacement for AssetModificationProcessor.OnWillSaveAssets 1 Answer
A weird issue in Unity related to scripts and Editor. 1 Answer