Search Unity

ShowObjectPicker freezes Unity

Discussion in 'Scripting' started by viduc, Oct 24, 2017.

  1. viduc

    viduc

    Joined:
    Oct 24, 2017
    Posts:
    3
    Hello,
    I've had a problem with ShowObjectPicker().
    The goal would be: as the user opens the TriggerChoiceWindow, the ObjectPicker window shows up, and the user can choose the object out of a selection. The chosen object would then be in an ObjectField.
    The problem is, using the ShowObjectPicker function freezes Unity.
    Here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. public class TriggerChoiceWindow : EditorWindow {
    7.  
    8.     [MenuItem("Window/TriggerChoice")]
    9.     public static void ShowWindow()
    10.     {
    11.         EditorWindow.GetWindow(typeof(TriggerChoiceWindow));
    12.     }
    13.  
    14.     int currentPickerWindow;
    15.     public Object selectedObject;
    16.  
    17.     void ShowPicker()
    18.     {
    19.         currentPickerWindow = EditorGUIUtility.GetControlID(FocusType.Passive);
    20.         EditorGUIUtility.ShowObjectPicker<Object>(null, false, "Cube", currentPickerWindow);
    21.     }
    22.  
    23.     void OnGUI()
    24.     {
    25.         ShowPicker();
    26.         if (Event.current.commandName == "ObjectSelectorUpdated" && EditorGUIUtility.GetObjectPickerControlID() == currentPickerWindow)
    27.         {
    28.             selectedObject = EditorGUIUtility.GetObjectPickerObject();
    29.             EditorGUILayout.ObjectField(selectedObject, typeof(Object), true);
    30.             currentPickerWindow = -1;
    31.             Debug.Log(selectedObject.name);
    32.         }
    33.     }
    34. }
    35.  
    Unity doesn't freeze instantly; it does try to display the selected object's name, but it throws a NullReferenceException. After several tries, I've found that it's the line with ShowObjectPicker that causes Unity to freeze. Without that line, Unity just works normally.

    I hope someone can help me. Thank you for your time.