Search Unity

Resolved SerializedObject of SerializedProperty has been Disposed

Discussion in 'Scripting' started by urbemAngeli, Oct 11, 2020.

  1. urbemAngeli

    urbemAngeli

    Joined:
    May 12, 2017
    Posts:
    61
    I have an InclinedPanel prefab, which inside contains another panel prefab (object Prefab). In the inspector window, I want to be able to change the panel's internal prefab to something else. Thus, the internal prefab of the panel should always remain a prefab. When you try to switch the panel type, the old panel is deleted and the new one is loaded. However, the panelType variable is not toggled and the error appears. I tried restarting Unity and the problem persists. I also tried to manually unpack the Prefab through the editor and delete it through the code. Everything worked as it should. It seems to me that the problem is in the incorrect deletion of the prefab. For a long time I tried to figure it out myself, but I already ran out of ideas. Please help me solve my problem.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. [CustomEditor(typeof(InclinedPanel))]
    7. public class InclinedPanelEditor : Editor
    8. {
    9.  
    10.     public SerializedProperty isGizmos;
    11.  
    12.     public SerializedProperty panelType;
    13.     public SerializedProperty isPortalable;
    14.     public SerializedProperty angle;
    15.     public SerializedProperty time;
    16.     public SerializedProperty isPrewarm;
    17.  
    18.     private GameObject main;
    19.     private InclinedPanel.PanelType oldPanelType;
    20.  
    21.  
    22.     private void OnEnable()
    23.     {
    24.         isGizmos = serializedObject.FindProperty("isGizmos");
    25.  
    26.         panelType = serializedObject.FindProperty("panelType");
    27.         isPortalable = serializedObject.FindProperty("isPortalable");
    28.         angle = serializedObject.FindProperty("angle");
    29.         time = serializedObject.FindProperty("time");
    30.         isPrewarm = serializedObject.FindProperty("isPrewarm");
    31.  
    32.         main = ((InclinedPanel)target).gameObject;
    33.         oldPanelType = (InclinedPanel.PanelType)panelType.enumValueIndex;
    34.  
    35.         // ------------------------------------------
    36.         if (PrefabUtility.IsAnyPrefabInstanceRoot(main))
    37.         {
    38.             PrefabUtility.UnpackPrefabInstance(main, PrefabUnpackMode.OutermostRoot, InteractionMode.AutomatedAction);
    39.         }
    40.     }
    41.  
    42.  
    43.     public override void OnInspectorGUI()
    44.     {
    45.         GUI.enabled = false;
    46.         EditorGUILayout.ObjectField("Script:", MonoScript.FromMonoBehaviour((InclinedPanel)target), typeof(InclinedPanel), false);
    47.         GUI.enabled = true;
    48.  
    49.         serializedObject.Update();
    50.  
    51.         EditorGUILayout.PropertyField(isGizmos);
    52.         EditorGUILayout.PropertyField(panelType);
    53.         if(panelType.enumValueIndex != (int)InclinedPanel.PanelType.Glass)
    54.             EditorGUILayout.PropertyField(isPortalable);
    55.         EditorGUILayout.PropertyField(angle);
    56.         EditorGUILayout.PropertyField(time);
    57.         EditorGUILayout.PropertyField(isPrewarm);
    58.  
    59.         if (panelType.enumValueIndex != (int)oldPanelType)
    60.             SetPanel((InclinedPanel.PanelType)panelType.enumValueIndex);
    61.  
    62.         oldPanelType = (InclinedPanel.PanelType)panelType.enumValueIndex;
    63.  
    64.         serializedObject.ApplyModifiedProperties();
    65.     }
    66.  
    67.  
    68.     private void SetPanel(InclinedPanel.PanelType panelType)
    69.     {
    70.         RemovePanel();
    71.  
    72.         string name = string.Empty;
    73.         if (panelType == InclinedPanel.PanelType.Glass)
    74.         {
    75.             name = "InclinedPanel_Glass";
    76.         }
    77.         else // Tile
    78.         {
    79.             if (isPortalable.boolValue)
    80.                 name = "InclinedPanel_Tile_white";
    81.             else
    82.                 name = "InclinedPanel_Tile_black";
    83.         }
    84.  
    85.         string path = "Assets/Prefabs/Panels/InclinedPanel/Templates/" + name + ".prefab";
    86.         Object temp = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
    87.         GameObject prefab = (GameObject)PrefabUtility.InstantiatePrefab(temp);
    88.         prefab.name = "Prefab";
    89.         prefab.transform.parent = main.transform;
    90.         prefab.transform.localPosition = Vector3.zero;
    91.         prefab.transform.localRotation = Quaternion.identity;
    92.     }
    93.    
    94.  
    95.     private void RemovePanel()
    96.     {
    97.         Debug.Log("Remove");
    98.         GameObject prefab = main.transform.Find("Prefab").gameObject;
    99.         PrefabUtility.UnpackPrefabInstance(prefab, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
    100.         DestroyImmediate(prefab);
    101.     }
    102. }
    103.  
     

    Attached Files:

  2. urbemAngeli

    urbemAngeli

    Joined:
    May 12, 2017
    Posts:
    61
    Wow! I managed to solve the problem, I just removed line 99 and now there are no errors.
     
    jmakar2021 likes this.
  3. sasabatu

    sasabatu

    Joined:
    Dec 18, 2020
    Posts:
    2
    I'm still getting this error and couldn't find the solution. I just started Unity and I'm doing everything as told in the tutorial.

    NullReferenceException: SerializedObject of SerializedProperty has been Disposed.
    UnityEditor.SerializedProperty.set_objectReferenceValue (UnityEngine.Object value) (at <fac3a832ec4249a49c4da1051848dde5>:0)
    UnityEngine.InputSystem.Editor.PlayerInputEditor+<>c__DisplayClass6_0.<DoHelpCreateAssetUI>b__0 () (at Library/PackageCache/com.unity.inputsystem@1.0.1/InputSystem/Plugins/PlayerInput/PlayerInputEditor.cs:285)
    UnityEditor.EditorApplication.Internal_CallDelayFunctions () (at <fac3a832ec4249a49c4da1051848dde5>:0)
     
    Sarty_Febor and ZE-Games like this.
  4. ZE-Games

    ZE-Games

    Joined:
    Jan 23, 2021
    Posts:
    1
    im following the rollable tutorial and i got this. (when i clicked "new actions" and went to put it in a folder named input")
    the specific video where this is done is this:


    this might be worth mentioning , but in the video , it was pre-named "rollable" , but for me , it was "rollable.inputactions"
     
  5. D4ni3l

    D4ni3l

    Joined:
    Dec 25, 2016
    Posts:
    1
    Hello guys,

    if you change from old input system to new input system you need to let the EventSystem know about it.
    Choose EventSystem (game object in your hierarchy) and in the properties window you will see a message (with a red sign) that you trying to use the wrong input system. There is a button under it to change to Inputsystem.UI or something like that. Hit that and you should be good.

    Hope this helps
     
  6. SC20K

    SC20K

    Joined:
    Jun 27, 2021
    Posts:
    1
    Hey if you're getting an error here- 1. Click your player object 2. Scroll down the hierarchy tab to "player input" 3. In the box next to "actions" click the little circle with a dot icon 4. Select "input actions," clear your error message and playtest to see if it comes back
     
    Thorrow, ryunosuke0114 and patrickgh3 like this.
  7. RomanRomanArbuz

    RomanRomanArbuz

    Joined:
    Jul 15, 2021
    Posts:
    1
    I have the same problem, still haven't solved it , followed all the steps from this tutorial.
     
  8. Cryoticen

    Cryoticen

    Joined:
    Oct 7, 2021
    Posts:
    1
    I "fixed it" by not using the debug inspector.
     
  9. rickygai

    rickygai

    Joined:
    Mar 9, 2022
    Posts:
    106
    Microsoft Windows 11 x64 Version 22H2 (OS Build 22621.1992)
    Unity 2022.3.5f1
    Unity Hub 3.5.0

    I found a simple reason that generated this error.

    For example, under Start() you initialized a class object "o":

    CFX o; // Global class "o".
    void Start()
    {
    o = GameObjecty.GetComponentInChildren<yourClass>();
    ...
    }

    Then under Update() you forgotten to check whether o is a null or not ?

    void Update()
    {
    if( o.Ready ) // This line will cause the error of "NullReferenceException: SerializedObject of SerializedProperty has been Disposed."
    {
    ...
    }
    }

    Hopes, this help.
     
    seifbensib and Herzio1995 like this.
  10. Herzio1995

    Herzio1995

    Joined:
    Jun 27, 2017
    Posts:
    1
    This worked for me.
    Thank you very much!
     
  11. Buchi0

    Buchi0

    Joined:
    Nov 2, 2018
    Posts:
    4
    Had the same problem. a public list of character names, dynamically updated during runtime. when i close the game, these errors keep scrolling trough the console. But not, twhen i do not have that script open in the inspector.
    My fix was to change that list to "private", and made some methods for accessing it from other Scripts. So the list is not shown in the inspector and i do not get that arror again.
    To, the conclusion is: There is a problem in the unity inspector when there is a public list displayed, whose contents are removed during runtime. I am using 2022.3.7f1
     
    davaban49 and Nomad1108 like this.
  12. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,823
    I'm getting this in Unity 2022.3.16f1, it doesn't even seem to point to the script that's causing the issues which is bit of a problem since I've just moved the project over to this version of unity.
     
  13. acjxn

    acjxn

    Joined:
    Dec 14, 2021
    Posts:
    2
    Thanks! This solves my problem!
     
  14. lazy_clap

    lazy_clap

    Joined:
    Jul 9, 2021
    Posts:
    8
    This fixed it for me, what a silly bug!