Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Bug NullReferenceException when changing Layer Mask Options in the inspector

Discussion in 'Editor & General Support' started by Mafiale, May 18, 2024.

  1. Mafiale

    Mafiale

    Joined:
    Apr 22, 2024
    Posts:
    4
    Hey everybody,

    I have a very simple script where I declare a public ContactFilter 2D so that I can adjust it in the inspector:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DetectCollision : MonoBehaviour
    6. {
    7.    public ContactFilter2D contactFilter;
    8.  
    9.    void Start()
    10.    {
    11.  
    12.    }
    13.  
    14. }
    This script is attached to a bullet prefab. If I now open the bullet prefab in the Inspector and want to actually change the layer mask options for raycasting in the component for the script I get an error as soon as I toggle/untoggle any option in the layer mask dropdown menu.

    Attached is a screenshot directly after I attach the script to the bullet prefab and then when I try to change the layer mask option.
    Unbenannt.PNG
    Unbenannt2.PNG

    This issue has also caused my editor to crash multiple times. Interestingly, this error does not appear when I attach the script to any other game object and change the layer mask options. Here is the full error I get:


    NullReferenceException: Object reference not set to an instance of an object
    UnityEditor.UIElements.Bindings.SerializedObjectBindingToBaseField`2[TValue,TField].OnFieldAttached () (at <d66e1a49bb1b4f56b6bbb21deaafcac2>:0)
    UnityEditor.UIElements.Bindings.SerializedObjectBindingBase.OnAttachToPanel (UnityEngine.UIElements.AttachToPanelEvent evt) (at <d66e1a49bb1b4f56b6bbb21deaafcac2>:0)
    UnityEngine.UIElements.EventCallbackFunctor`1[TEventType].Invoke (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.PropagationPhase propagationPhase) (at <fef112fd0f15457e8e58500118cb5cea>:0)
    UnityEngine.UIElements.EventCallbackRegistry.InvokeCallbacks (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.PropagationPhase propagationPhase) (at <fef112fd0f15457e8e58500118cb5cea>:0)
    UnityEngine.UIElements.CallbackEventHandler.HandleEvent (UnityEngine.UIElements.EventBase evt) (at <fef112fd0f15457e8e58500118cb5cea>:0)
    UnityEngine.UIElements.CallbackEventHandler.HandleEventAtCurrentTargetAndPhase (UnityEngine.UIElements.EventBase evt) (at <fef112fd0f15457e8e58500118cb5cea>:0)
    UnityEngine.UIElements.CallbackEventHandler.HandleEventAtTargetPhase (UnityEngine.UIElements.EventBase evt) (at <fef112fd0f15457e8e58500118cb5cea>:0)
    UnityEngine.UIElements.CallbackEventHandler.HandleEventAtTargetAndDefaultPhase (UnityEngine.UIElements.EventBase evt) (at <fef112fd0f15457e8e58500118cb5cea>:0)
    UnityEngine.UIElements.VisualElement.HasChangedPanel (UnityEngine.UIElements.BaseVisualElementPanel prevPanel) (at <fef112fd0f15457e8e58500118cb5cea>:0)
    UnityEngine.UIElements.VisualElement.SetPanel (UnityEngine.UIElements.BaseVisualElementPanel p) (at <fef112fd0f15457e8e58500118cb5cea>:0)
    UnityEngine.UIElements.VisualElement+Hierarchy.SetParent (UnityEngine.UIElements.VisualElement value) (at <fef112fd0f15457e8e58500118cb5cea>:0)
    UnityEngine.UIElements.VisualElement+Hierarchy.Insert (System.Int32 index, UnityEngine.UIElements.VisualElement child) (at <fef112fd0f15457e8e58500118cb5cea>:0)
    UnityEngine.UIElements.VisualElement+Hierarchy.Add (UnityEngine.UIElements.VisualElement child) (at <fef112fd0f15457e8e58500118cb5cea>:0)
    UnityEditor.UIElements.PropertyField.Reset (UnityEditor.SerializedProperty newProperty) (at <477bdcb097d4488eae58cbed17286dd8>:0)
    UnityEditor.UIElements.PropertyField.Reset (UnityEditor.UIElements.SerializedPropertyBindEvent evt) (at <477bdcb097d4488eae58cbed17286dd8>:0)
    UnityEditor.UIElements.PropertyField.ExecuteDefaultActionAtTarget (UnityEngine.UIElements.EventBase evt) (at <477bdcb097d4488eae58cbed17286dd8>:0)
    UnityEngine.UIElements.CallbackEventHandler.HandleEvent (UnityEngine.UIElements.EventBase evt) (at <fef112fd0f15457e8e58500118cb5cea>:0)
    UnityEngine.UIElements.CallbackEventHandler.HandleEventAtCurrentTargetAndPhase (UnityEngine.UIElements.EventBase evt) (at <fef112fd0f15457e8e58500118cb5cea>:0)
    UnityEngine.UIElements.CallbackEventHandler.HandleEventAtTargetPhase (UnityEngine.UIElements.EventBase evt) (at <fef112fd0f15457e8e58500118cb5cea>:0)
    UnityEngine.UIElements.CallbackEventHandler.HandleEventAtTargetAndDefaultPhase (UnityEngine.UIElements.EventBase evt) (at <fef112fd0f15457e8e58500118cb5cea>:0)
    UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.SendBindingEvent[TEventType] (TEventType evt, UnityEngine.UIElements.VisualElement target) (at <d66e1a49bb1b4f56b6bbb21deaafcac2>:0)
    UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindPropertyRelative (UnityEngine.UIElements.IBindable field, UnityEditor.SerializedProperty parentProperty) (at <d66e1a49bb1b4f56b6bbb21deaafcac2>:0)
    UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindTree (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at <d66e1a49bb1b4f56b6bbb21deaafcac2>:0)
    UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindTree (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at <d66e1a49bb1b4f56b6bbb21deaafcac2>:0)
    UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindTree (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at <d66e1a49bb1b4f56b6bbb21deaafcac2>:0)
    UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.ContinueBinding (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at <d66e1a49bb1b4f56b6bbb21deaafcac2>:0)
    UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.Bind (UnityEngine.UIElements.VisualElement element) (at <d66e1a49bb1b4f56b6bbb21deaafcac2>:0)
    UnityEditor.UIElements.Bindings.DefaultSerializedObjectBindingImplementation.Bind (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedObject obj) (at <d66e1a49bb1b4f56b6bbb21deaafcac2>:0)
    UnityEditor.UIElements.BindingExtensions.Bind (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedObject obj) (at <477bdcb097d4488eae58cbed17286dd8>:0)
    UnityEditor.UIElements.InspectorElement.SetEditor (UnityEditor.Editor value) (at <477bdcb097d4488eae58cbed17286dd8>:0)
    UnityEditor.UIElements.EditorElement.Reinit (System.Int32 editorIndex) (at <477bdcb097d4488eae58cbed17286dd8>:0)
    UnityEditor.PropertyEditor.ProcessEditorElementsToRebuild (UnityEditor.Editor[] editors) (at <477bdcb097d4488eae58cbed17286dd8>:0)
    UnityEditor.PropertyEditor.DrawEditors (UnityEditor.Editor[] editors) (at <477bdcb097d4488eae58cbed17286dd8>:0)
    UnityEditor.PropertyEditor.RebuildContentsContainers () (at <477bdcb097d4488eae58cbed17286dd8>:0)
    UnityEditor.InspectorWindow.RedrawFromNative () (at <477bdcb097d4488eae58cbed17286dd8>:0)
    UnityEditor.ObjectChangeEvents:InvokeChangeEvent(IntPtr, Int32, IntPtr, Int32)
     
    Last edited: May 18, 2024
  2. samana1407

    samana1407

    Joined:
    Aug 23, 2015
    Posts:
    285
    I tried to recreate the issue myself, but it didn't happen. Everything is working fine without any errors.
     
  3. Mafiale

    Mafiale

    Joined:
    Apr 22, 2024
    Posts:
    4
    Found a solution. Here is when the error occured:

    When I single clicked the bullet prefab in the project window then attached the script and then changed the layer mask options I got the error.

    However, when I double click the bullet prefab in the project window (so the scene view and hierarchy windows are inside the prefab) and then repeat the steps I get no error.

    I am slightly confused as I never had problems changing prefabs without double clicking them. Is this intended behaviour?

    For instance I am able to change other properties from other scripts (projectile script for instance) just fine when I single click the bullet prefab and then change it directly from the inspector.