Assignment is not being made to the variable of type 'ObjectField'.

I’m creating a custom window with the UIBuilder. Inside the window, there is an ObjectField element that I’m using.
I’m making the assignment in this way.

savedTasksObjectField = root.Q<ObjectField>("tasksObjectField");

However, the variable never takes the ObjectField element inside. It always returns a null value.
There is no error in the naming. When I reference it with the same name and assign it to a variable of type VisualElement, the assignment process takes place successfully. However, when I cast the assigned VisualElement variable to an ObjectField variable, the ObjectField still returns a null value.

savedTasksVisualElement = root.Q<VisualElement>("tasksObjectField");
savedTasksObjectField = savedTasksVisualElement as ObjectField;

An assignment operation is never performed to an element of type ObjectField.

Unity Version : 2022.3.5f1

How does the field look in the hierarchy when you analyze it with UIToolkit’s debugger (ctrl+F5)?

When is the assignment happening? OnEnable? CreateGUI?

It may depend if you’re querying the root visual element before the visual tree has been built yet.

I tried the assignment in both functions but nothing changed.

public void CreateGUI()
        {
            container = rootVisualElement;

            VisualTreeAsset visualTree =
                AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/CustomEditor/Editor/TaskListEditor.uxml");
            container.Add(visualTree.Instantiate());

            StyleSheet styleSheet =
                AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/CustomEditor/Editor/TaskListEditor.uss");
            container.styleSheets.Add(styleSheet);

            savedTasksObjectField = container.Q<ObjectField>("tasksObjectField");

            loadTasksButton = container.Q<Button>("loadTaskButton");
            loadTasksButton.clicked += LoadTasks;
            loadTasksButton.focusable = false;

            taskText = container.Q<TextField>("taskText");
            taskText.RegisterCallback<KeyDownEvent>(AddTask);

            addTaskButton = container.Q<Button>("addTaskButton");
            addTaskButton.clicked += AddTask;
            addTaskButton.focusable = false;

            taskListScrollView = container.Q<ScrollView>("taskList");
            taskListScrollView.visible = false;
        }

It’s working now. It worked without making any changes.
I’ve been dealing with the same issue since yesterday. I’ve tried the same things multiple times. I still don’t know what’s causing the problem.

It really seems to me like it should always work. The only thing that occurs to me is that ObjectField doesn’t mean the same in your code than in your UXML. That maybe you have your own ObjectField class, or some library somewhere. If that’s not it, maybe there’s a bug in Unity, but I use queries like that a lot and I haven’t found that problem.

If it happens again, maybe the next step would be something like this:

savedTasksVisualElement = root.Q<VisualElement>("tasksObjectField");

Debug.Log(savedTasksVisualElement.GetType().AssemblyQualifiedName)

Debug.Log(typeof(ObjectField).AssemblyQualifiedName);

If both logs are the same, there’s probably a bug in Unity’s code. If they are different, you now have a thread to follow.

Thank you for the help.

When I apply the code you have provided, I am getting these outputs:

Hi. I recommend quoting or tagging when replying in the forum. When you do that, I get an alert so I know you replied to me. : )

Ahm. About that. They look to be exactly the same. Are these Logs done with a working example? If the example is broken again, these logs would indicate that Unity’s query system has a bug. If the example is working, then this is to be expected.

1 Like

Yes, you’re right, I apologize for not quoting.

Yes, I’ve tried it on my own project. Currently, there is no issue. Assignment to the ObjectField variable is being done successfully.
I’m certain that the assignment is done correctly when it gives an error. In the screenshot I provided initially, the name in the hierarchy is visible. Even though the naming is the same, I can’t understand why the assignment isn’t being made.

I haven’t had a chance to test the project on another version. However, I believe there might be an issue related to the version I’m using (2022.3.5)

1 Like

I came across this issue and found a fix to try, in case others happen by this issue as well.

My issue came down to the IDE adding in the ‘using UnityEditor.Search’ instead of ‘using UnityEditor.UIElements’. Both of these have a ObjectField and the query won’t flag using the Search class as an error.

TLDR: Make sure to include ‘using UnityEditor.UIElements;’