Editor Error when selecting prefab in scene

Hi everyone,

I’ve being trying implementing UI Elements and UIBuilder in my project for a better editor experience. Now a got an error every time y have the prefab object selected in the scene hierarchy.

Everything happened when I tried to add a foldout element in my editor extension class, something broke in the editor and now a can’t get rid of this error. This is the code in the editor extension class:

    [CustomEditor(typeof(Brain))]
    [CanEditMultipleObjects]
    public class BrainEditor<T> : UnityEditor.Editor
    {
        private Brain _brain;
        private VisualElement rootElement;
        public VisualTreeAsset visualTreeAsset;
        private void OnEnable()
        {
            _brain = target as Brain;
            rootElement = new VisualElement(); //Parent element of the layout

            visualTreeAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Scripts/Editor/BrainEditor/BrainEditor.uxml");
            StyleSheet uss = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Scripts/Editor/BrainEditor/BrainEditor.uss");
            AssetDatabase.Refresh();
            rootElement.styleSheets.Add(uss);
        }

        public override VisualElement CreateInspectorGUI()
        {
            VisualElement root = rootElement;
            root.Clear();

            visualTreeAsset.CloneTree(root);


            
            Slider currentDifficultySlider = root.Q<Slider>(name: "CurrentDifficulty");
            currentDifficultySlider.value = _brain.difficulty;

            Label currentDifficultyDisplay = root.Q<Label>(name: "CurrentDifficultyDisplay");
            currentDifficultyDisplay.text = _brain.difficulty.ToString();
            
            //THIS IS WHEN ERORR STARTED TO HAPPENED
            //Foldout restfullFoldout = root.Q<Foldout>(name: "RestFullModesPool");
            //restfullFoldout.Bind(serializedObject.targetObjects.)
            return root;
        }


    }

This is what it says the console every time a keep this prefab selected in the scene hierarchy:

NullReferenceException: Object reference not set to an instance of an object
UnityEditor.InspectorWindow.DrawEditors (UnityEditor.Editor[ ] editors) (at <7d90b28560b644ce87b8263efcc36eed>:0)
UnityEditor.InspectorWindow.RebuildContentsContainers () (at <7d90b28560b644ce87b8263efcc36eed>:0)
UnityEditor.InspectorWindow.RedrawFromNative () (at <7d90b28560b644ce87b8263efcc36eed>:0)
GUI Error: You are pushing more GUIClips than you are popping. Make sure they are balanced.
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
NullReferenceException: Object reference not set to an instance of an object
UnityEditor.SceneView.CallOnPreSceneGUI () (at <7d90b28560b644ce87b8263efcc36eed>:0)
UnityEditor.SceneView.DoOnPreSceneGUICallbacks (UnityEngine.Rect cameraRect) (at <7d90b28560b644ce87b8263efcc36eed>:0)
UnityEditor.SceneView.OnGUI () (at <7d90b28560b644ce87b8263efcc36eed>:0)
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[ ] parameters, System.Globalization.CultureInfo culture) (at <9577ac7a62ef43179789031239ba8798>:0)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[ ] parameters, System.Globalization.CultureInfo culture) (at <9577ac7a62ef43179789031239ba8798>:0)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[ ] parameters) (at <9577ac7a62ef43179789031239ba8798>:0)
UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at <7d90b28560b644ce87b8263efcc36eed>:0)
UnityEditor.HostView.Invoke (System.String methodName) (at <7d90b28560b644ce87b8263efcc36eed>:0)
UnityEditor.HostView.InvokeOnGUI (UnityEngine.Rect onGUIPosition, UnityEngine.Rect viewRect) (at <7d90b28560b644ce87b8263efcc36eed>:0)
UnityEditor.DockArea.DrawView (UnityEngine.Rect viewRect, UnityEngine.Rect dockAreaRect) (at <7d90b28560b644ce87b8263efcc36eed>:0)
UnityEditor.DockArea.OldOnGUI () (at <7d90b28560b644ce87b8263efcc36eed>:0)
UnityEngine.UIElements.IMGUIContainer.DoOnGUI (UnityEngine.Event evt, UnityEngine.Matrix4x4 parentTransform, UnityEngine.Rect clippingRect, System.Boolean isComputingLayout, UnityEngine.Rect layoutSize, System.Action onGUIHandler, System.Boolean canAffectFocus) (at <06214b245dbb4d10a9cefd10639bb04e>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)

I’ve tried commenting the line of code on witch the error started showing up, erasing the file, uninstalling UIBuilder package and installing it again several times, creating again the UXML file for the project and deleting temp and library folders but still the same error. i’ve search on forums but nothing quite simillar to this or I couldn’t find any suitable solution.

Anyone has an idea on how to solve this issue?

Thanks very much for you time, any suggestion would be very much appreciated.

Finnaly I got the issue. My bad, the conflict came by the generic type of the class I tried to implement, but never used. Don’t know why, but this caused the conflict on the editor.