Object picking from the scene view

Hi,

I am surprised that question haven’t been asked before. Or my search skills are horrible (Probably).

I want to know if there is a shortcut or anything to pick objects from the scene view instead of having to using the “Select GameObject” dialog or to drag it from the Hierarchy.

This is really frustrating when you have a level with lot of doors and triggers.

Thanks!

Unfortunately there’s no such function, but you can work with two inspector windows if it’S really a big scene. That way you can select your object with the script so you see the variable in the inspector and then lock one inspector so it stil shows the script even when you select another object.

Now you can simply click on another object in the scene view so it get highlighted in the projectview and you can drag it from the project view to the variable (which is still visible in the locked inspector window)

To lock an inspector window you just have to click the little pad-lock-icon at the top of the window.

This is also the only way to assign a specific component to another object when there are more than one of these components on this object.

edit

I’ve just created an EditorWindow which allows you to pick the object under the cursor in the scene view. The picked object is shown in the custom window and you can start any kind of a drag & drop operation from there. It supports a “history stack” of the last picked objects if you want :wink:

This script has to be named “SceneViewObjectWindow.cs” and should be placed in “Assets/editor/”.

Just open the window via the Tools menu and dock it near the inspector window. To pick an object i’ve assigned the hotkey “ALT + S” (%s). You can modify it if you want a different hotkey (See [MenuItem][1] for more information).

//SceneViewObjectWindow.cs
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

public class SceneViewObjectWindow : EditorWindow
{
    static GameObject m_LastObject;    
    static List<GameObject> m_Stack = new List<GameObject>();
    
    static bool m_UseStack = false;
    static float m_MaxStackSize = 5;
    
    [MenuItem("Tools/Open SceneView Object Selector")]
    public static void OpenWindow()
    {
        EditorWindow.GetWindow<SceneViewObjectWindow>();
    }
    
    [MenuItem("Tools/Select Scene Object &s")]
    public static void SelectObject()
    {
        if (m_LastObject != null)
        {
            if (!m_UseStack)
                m_Stack.Clear();
            m_Stack.Add(m_LastObject);
        }
        EditorWindow.GetWindow<SceneViewObjectWindow>().Repaint();
    }
    
    void OnGUI()
    {
        Event e = Event.current;
        m_UseStack = GUILayout.Toggle(m_UseStack,"Use Stack");
        if (m_UseStack)
            m_MaxStackSize = GUILayout.HorizontalSlider(m_MaxStackSize,1,20);
        
        for(int i = m_Stack.Count-1;i>=0;i--)
        {
            GUILayout.BeginHorizontal();
            EditorGUILayout.ObjectField(m_Stack*,typeof(GameObject));*

if (GUILayoutUtility.GetLastRect().Contains(e.mousePosition) && e.type == EventType.MouseDrag)
{
DragAndDrop.PrepareStartDrag ();
DragAndDrop.objectReferences = new UnityEngine.Object {m_Stack*};
DragAndDrop.StartDrag (“drag”);
Event.current.Use();
_
}_
if(GUILayout.Button(“X”,GUILayout.Width(20)))
_
{_
m_Stack.RemoveAt(i);
Repaint();
_
}_
GUILayout.EndHorizontal();
if (e.type == EventType.Repaint && m_Stack == null)
_{
m_Stack.RemoveAt(i);
Repaint();
}
}
if (m_UseStack && e.type == EventType.Repaint)
{
while(m_Stack.Count > m_MaxStackSize)
m_Stack.RemoveAt(0);
}
}*_

[DrawGizmo(GizmoType.NotSelected)]
static void RenderCustomGizmo(Transform objectTransform, GizmoType gizmoType)
{
if (Event.current == null)
return;
Ray ray = HandleUtility.GUIPointToWorldRay (Event.current.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit))
{
m_LastObject = hit.transform.gameObject;
}
}
}
_*[1]: http://unity3d.com/support/documentation/ScriptReference/MenuItem.html*_