- Home /
Select next object in Hierarchy?
By default, you can change the currently selected GameObject in the hierarchy by using the arrow keys to move the current selection up or down. I would like to recreate this behavior in my own code so I can select objects relative to (above or below) the current selection. However, I'm not sure how best to do this.
I know you can already use the sibling index to do something like this:
int index = Selection.activeGameObject.transform.GetSiblingIndex();
Selection.activeGameObject = transform.parent.GetChild(index + 1).gameObject;
However, using the sibling index only changes the selection relative to other siblings, but I want it to work just like the default hierarchy where this is ignored and you simply select whatever is above or below the currently selected object in the hierarchy, regardless of parent / child relationship.
I'm guessing I need to use the TreeView class for this operation?
I think your almost there, the only extra condition I can think that you have to consider is if the current selected transform has a child (since that will be selected before the next sibling).
Try:
int index = Selection.activeGameObject.transform.GetSiblingIndex();
if (Selection.activeGameObject.transform.childCount > 0)
Selection.activeGameObject = Selection.activeGameObject.transform.GetChild(0);
else
Selection.activeGameObject = transform.parent.GetChild(index + 1).gameObject;
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Fold/unfold gameobject from code 4 Answers
Making a list selection variable 1 Answer
Editor Script Selection thinks Sprites are Texture2Ds. 2 Answers
Editor scripting, select a field 1 Answer