Selecting in scene: select as if clicked the object in hierarchy

Selection.activeInstanceID = someGameObject.GetInstanceID();
Selection.activeGameObject = someGameObject;

This doesn’t select a gameObject like it would when you click in the hierarchy (it’s greyed out in the hierarchy)
Like this:

But when you click an object in the hierarchy as opposed to the scene view, it becomes blue and you can rename with F2.
Like this:

I would like to make it so that when I select something in the scene it becomes blue and not grey. Through code. And thus I would be able to rename with the hotkey F2 my scene selected objects.

It looks like you just need to bring focus to the HierarchyWindow

EditorApplication.ExecuteMenuItem("Window/Hierarchy")

Thanks, that works perfectly! You’re the man, Naster :wink:

Glad I could help :slight_smile:

Hey Naster, I’ve been trying to switch from 2d to 3d view when I’m in the Hierarchy window.
Unity has a shortcut for this with Alpha2 in the scene view but this one does not work when the focus is on the Hierarchy window.

if (e.keyCode.Equals(KeyCode.Alpha1))
                {
                    if (!e.alt)
                        if (!e.control)
                            if (!e.shift)
                            {
                                //SceneView. = SceneView.lastActiveSceneView;
                                //SceneView. = SceneView.currentDrawingSceneView;
                                EditorApplication.ExecuteMenuItem("Window/Scene");
                                SceneView.lastActiveSceneView.in2DMode =
                                    !SceneView.currentDrawingSceneView.in2DMode;
                            }
                }

So I tried it to create it this way, but it gives me an error when I have an object selected because the SceneView.lastActiveSceneView is not set to an instance of an object yet.
However, when I click the hotkey a 2nd time it works fine.
I’m trying to make it work in one go, do you have any idea how?

It might be due to the fact that
EditorApplication.ExecuteMenuItem takes time to execute? I’m not sure.

PS: The reason I have this problem in the first place is because I’m bringing the focus to the Hierarchy Window from the scene view when manipulating objects to have them highlighted in blue, I’d like to keep that.
This works pretty well, but when I first launch Unity I must always click at least once in the Hierarchy window to make the items be highlighted in blue when I’m moving them in the Scene. Do you know why ?

Hopefully this message is comprehensible!
Jeremy.