Select and deselect in Editor via C#

Hey guys!

I’ve been searching for this a bit now but I can’t seem to figure out how to do it.

I’d like to write a tool that deselects the currently selected GameObjects in the Editor (Scene View) and instead selects their parents.

While I can store the selected GOs in an array using GameObject[] selection = Selection.gameObjects and I know how to figure out their parents (parent = selection*.transform.parent), I don’t know how to make them being selected in the Scene View (since Selection.gameObjects is read-only).*
Thanks in advance! :smiley:
Best regards.

I wrote a script like that a while ago:

	[MenuItem ("Toolbox/Select direct parents %+")]
	public static void SelectParents () {
		GameObject[] objs = Selection.gameObjects;
		List<GameObject> parents = new List<GameObject>();
		foreach (GameObject obj in objs) {
			if (obj.transform.parent != null) {
				parents.Add(obj.transform.parent.gameObject);
			}
		}
		Selection.objects = parents.ToArray();
	}