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.
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.
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:
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.
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)
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;’