- Home /
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[i].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! :D
Best regards.
Answer by Philipp · Mar 06, 2013 at 07:41 AM
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();
}
Hey, thanks a lot!
That was precisely what I was looking for. :D
Your answer
Follow this Question
Related Questions
selecting and organising objects in editor 0 Answers
Fold/unfold gameobject from code 4 Answers
How to get the top most GameObject selected in Editor 0 Answers
Selecting objects in scene view with left mouse button always selects child object. 1 Answer
Horrible lag when selection objects in Editor hierarchy 0 Answers