ObjectField in a ListView is not working.

I test the following code:

using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

public class ObjectFieldTest : EditorWindow
{
    [MenuItem("Test/ObjectFieldTest", false, 10000)]
    private static void OpenWindow()
    {
        var window = GetWindowWithRect<ObjectFieldTest>(
            new Rect(0, 0, 500, 400));
        window.titleContent = new GUIContent("ObjectFieldTest");
        window.Show();
    }

    private void OnEnable()
    {
        var root = this.rootVisualElement;

        var listView = new ListView(new int[1])
        {
            reorderMode = ListViewReorderMode.Animated,
            makeItem = () =>
            {
                return new ObjectField("Source")
                {
                    allowSceneObjects = false,
                    objectType = typeof(Object),
                };
            },
            bindItem = (root, index) => { },
            fixedItemHeight = EditorGUIUtility.singleLineHeight,
        };

        root.Add(listView);
    }
}

And when I open the window and drop any object to the ObjectField, the unity report error:

NullReferenceException: Object reference not set to an instance of an object
UnityEngine.UIElements.ListViewDragger.MakeDragAndDropArgs (UnityEngine.UIElements.ListViewDragger+DragPosition dragPosition) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.UIElements.ListViewDragger.OnDrop (UnityEngine.Vector3 pointerPosition) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.UIElements.ListViewDraggerAnimated.OnDrop (UnityEngine.Vector3 pointerPosition) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.UIElements.DragEventsProcessor.OnDragPerformEvent (UnityEngine.UIElements.DragPerformEvent evt) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.UIElements.EventCallbackFunctor`1[TEventType].Invoke (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.PropagationPhase propagationPhase) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.UIElements.EventCallbackRegistry.InvokeCallbacks (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.PropagationPhase propagationPhase) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.UIElements.CallbackEventHandler.HandleEvent (UnityEngine.UIElements.EventBase evt) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.UIElements.EventDispatchUtilities.PropagateEvent (UnityEngine.UIElements.EventBase evt) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.UIElements.MouseEventDispatchingStrategy.SendEventToRegularTarget (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.BaseVisualElementPanel panel) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.UIElements.MouseEventDispatchingStrategy.SendEventToTarget (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.BaseVisualElementPanel panel) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.UIElements.MouseEventDispatchingStrategy.DispatchEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel iPanel) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.UIElements.EventDispatcher.ApplyDispatchingStrategies (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, System.Boolean imguiEventIsInitiallyUsed) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.UIElements.EventDispatcher.Dispatch (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, UnityEngine.UIElements.DispatchMode dispatchMode) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.UIElements.BaseVisualElementPanel.SendEvent (UnityEngine.UIElements.EventBase e, UnityEngine.UIElements.DispatchMode dispatchMode) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.UIElements.UIElementsUtility.DoDispatch (UnityEngine.UIElements.BaseVisualElementPanel panel) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.UIElements.UIElementsUtility.UnityEngine.UIElements.IUIElementsUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr, System.Boolean& eventHandled) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.UIElements.UIEventRegistration.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.UIElements.UIEventRegistration+<>c.<.cctor>b__1_2 (System.Int32 i, System.IntPtr ptr) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
UnityEngine.GUIUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr, System.Boolean& result) (at <ae7f5af45fd74d6f8ef88ea586136af6>:0)

I submit a bug(Case 1369139), and attach the test project.

7531817–929891–ObjectFieldTest.zip (19.1 KB)

4 Likes

I have the same issue.
In the UI Toolkit Event Debugger it shows that “DragPerformEvent” has the Target : “” that looks weird, but I don’t know if it has something to do with the problem.

Anyway it has something to do with list reorder mode : “Animated”. If you switch to “Simple” reorder mode, the problem disappears. So looks like a Unity bug.

Anyway, if you find anything else please share :slight_smile:

2 Likes

Thanks for the bug report we will look at it.

2 Likes

Is there any progress about this bug? It still happens in 2021.2.12f1.

1 Like

A fix was merged into 2021.2 a week ago.

2 Likes