Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug NullReferenceException: SerializedObject of SerializedProperty has been Disposed.

Discussion in 'Scripting' started by Ashiroen, Oct 7, 2023.

  1. Ashiroen

    Ashiroen

    Joined:
    Jul 9, 2020
    Posts:
    1
    Hello everyone,

    the following code seems to work just fine, however whenever I select the object with this script attached after stopping the previews, the following error gets spammed in the console:

    Code (CSharp):
    1. NullReferenceException: SerializedObject of SerializedProperty has been Disposed.
    2. UnityEditor.SerializedProperty.get_objectReferenceInstanceIDValue () (at <fe7039efe678478d9c83e73bc6a6566d>:0)
    3. UnityEditor.EditorGUIUtility.ObjectContent (UnityEngine.Object obj, System.Type type, UnityEditor.SerializedProperty property, UnityEditor.EditorGUI+ObjectFieldValidator validator) (at <fe7039efe678478d9c83e73bc6a6566d>:0)
    4. UnityEditor.UIElements.ObjectField+ObjectFieldDisplay.Update () (at <fe7039efe678478d9c83e73bc6a6566d>:0)
    5. UnityEditor.UIElements.ObjectField.UpdateDisplay () (at <fe7039efe678478d9c83e73bc6a6566d>:0)
    6. UnityEngine.UIElements.VisualElement+SimpleScheduledItem.PerformTimerUpdate (UnityEngine.UIElements.TimerState state) (at <79c7b132c51745cbae03eebea8111c0e>:0)
    7. UnityEngine.UIElements.TimerEventScheduler.UpdateScheduledEvents () (at <79c7b132c51745cbae03eebea8111c0e>:0)
    8. UnityEngine.UIElements.UIElementsUtility.UnityEngine.UIElements.IUIElementsUtility.UpdateSchedulers () (at <79c7b132c51745cbae03eebea8111c0e>:0)
    9. UnityEngine.UIElements.UIEventRegistration.UpdateSchedulers () (at <79c7b132c51745cbae03eebea8111c0e>:0)
    10. UnityEditor.RetainedMode.UpdateSchedulers () (at <c91a25f185b743118a39aafa100dff09>:0)
    11.  
    The associated script code is:
    Code (CSharp):
    1. public class Layout : MonoBehaviour
    2. {
    3.     [field:MinMaxSlider(0,100)] public Vector2Int SizeRange;
    4.     [field:MinMaxSlider(0,25)] public Vector2Int DistanceRange;
    5.     [field:SerializeField] public MonoBehaviour Generator {get; private set;}
    6.     [field:SerializeField] public List<GameObject> Nodes = new List<GameObject>();
    7.     [field:SerializeField] public List<GameObject> Connections = new List<GameObject>();
    8.     [field:SerializeField] public GameObject NodePrefab;
    9.     [field:SerializeField] public GameObject ConnectionPrefab;
    10.  
    11.     void Start()
    12.     {
    13.         int randomSize = Random.Range(SizeRange[0],SizeRange[1]);
    14.         while(Nodes.Count < randomSize){
    15.             generateNode(transform.position);
    16.         }
    17.     }
    18.  
    19.     void generateNode(Vector3 position){
    20.         GameObject newNode = Instantiate(NodePrefab,position,Quaternion.identity);
    21.         Nodes.Add(newNode);
    22.     }
    23. }
    24.  
    I have no clue what's going on and would appreciate anyone at least explaining.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,019
    What do you mean by that?

    If, for instance, you have a preview scene that you instantiate the objects into, then all references (particularly in Nodes) will likely remain in your lists even when the preview scene is disposed. In that case all the list objects are disposed too. But they aren‘t null, only the C++ object is disposed but the C# object remains a valid C# instance except Unity flags this as a disposed object.

    You need to explicitly clear these lists and null the references when you end the preview. The exception is probably raised by the Inspector trying to access the GameObject references which have been disposed, meaning they are no longer in any scene but still referenced by your script.