I am trying to create a editor Tool for testings of my dialog system, and I need to assign a button to test this functions, but for some reason I cant assign an function for the clicked event.
The createGUI function looks like this
public void CreateGUI()
{
VisualElement root = rootVisualElement;
TextInput = rootVisualElement.Q<TextField>("TextData");
DialogData = rootVisualElement.Q<ObjectField>("DialogSO");
BotaoTrigger = rootVisualElement.Q<Button>("Ativador");
DialogData.RegisterValueChangedCallback(Value => { dialog = (DialogDataSO)Value.newValue; });
//BotaoTrigger.RegisterCallback<>
BotaoTrigger.clicked += TypeTest;
// Instantiate UXML
VisualElement labelFromUXML = m_VisualTreeAsset.Instantiate();
root.Add(labelFromUXML);
}
In Visual Studio, it doesnāt show any type of errors, But after saving it and Unity Editor Compile the code, It returns a NullReferenceException, even when all the names are correct.
I really thank the help so far if itās not a bug
Thereās only one answer here: fix this first.
How to fix a NullReferenceException error
https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/
Three steps to success:
Identify what is null ā any other action taken before this step is WASTED TIME
Identify why it is null
Fix that
If the nullref isnāt in your code, then move onto other checks to ensure youāre using the library correctly.
Kurt-Dekker:
Thereās only one answer here: fix this first.
How to fix a NullReferenceException error
https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/
Three steps to success:
Identify what is null ā any other action taken before this step is WASTED TIME
Identify why it is null
Fix that
If the nullref isnāt in your code, then move onto other checks to ensure youāre using the library correctly.
The Null Reference is in the line that assigns the function do the clicked event.
This line delivers the button object with the button visual in the UIDocument.
BotaoTrigger = rootVisualElement.Q<Button>("Ativador");
And this live subscribe the function to the Clicked event of said button
BotaoTrigger.clicked += TypeTest;
The string does not have any typo or anything like it. It just send a null reference, there isnāt any reference to another script
If you just rootVisualElement.Q<Button>();
do you get any results?
1 Like
Since the default parameter for the Q() is null, it returns the same error in the same line (BotaoTrigger.clicked += TypeTest; )
So are there any UI elements actually generated at this point? It sounds like thereās just a root visual element and nothing else.
1 Like
Thatās why I believe is actually a Bug, because I made the UI Button In the UI document already, but aparently it wasnāt able to identify it, even with the right name
Right but has the button actually been added?
If I create I visual tree asset with a button called āTestButtonā, and write the following code:
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
public class EditorWindowTest : EditorWindow
{
[MenuItem("Testing/EditorWindowTest")]
public static void ShowExample()
{
EditorWindowTest wnd = GetWindow<EditorWindowTest>();
wnd.titleContent = new GUIContent("EditorWindowTest");
}
public void CreateGUI()
{
VisualElement root = rootVisualElement;
var visualAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("PathToAsset");
VisualElement treeRoot = visualAsset.Instantiate();
root.Add(treeRoot);
Button button = treeRoot.Q<Button>("TestButton");
button.clicked += () => Debug.Log("Working!");
}
}
It works totally fine:
Notably I have to load and add the visual tree first before I can query the button. They arenāt magically tied together.
1 Like
spiney199:
Right but has the button actually been added?
If I create I visual tree asset with a button called āTestButtonā, and write the following code:
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
public class EditorWindowTest : EditorWindow
{
[MenuItem("Testing/EditorWindowTest")]
public static void ShowExample()
{
EditorWindowTest wnd = GetWindow<EditorWindowTest>();
wnd.titleContent = new GUIContent("EditorWindowTest");
}
public void CreateGUI()
{
VisualElement root = rootVisualElement;
var visualAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("PathToAsset");
VisualElement treeRoot = visualAsset.Instantiate();
root.Add(treeRoot);
Button button = treeRoot.Q<Button>("TestButton");
button.clicked += () => Debug.Log("Working!");
}
}
It works totally fine:
Notably I have to load and add the visual tree first before I can query the button. They arenāt magically tied together.
Thanks for the answer, Since the other elements seemed to work correctly (Or I was thinking too much in the old editor window way to do it), then I believed there was a bug actualy. Now it seems to work fine (Good that I know that I need to look better at the other elements as well).