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?

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 {

}

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 :slight_smile:

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;
       
    }
}