SerializedObject of SerializedProperty has been Disposed

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(InclinedPanel))]
public class InclinedPanelEditor : Editor
{

    public SerializedProperty isGizmos;

    public SerializedProperty panelType;
    public SerializedProperty isPortalable;
    public SerializedProperty angle;
    public SerializedProperty time;
    public SerializedProperty isPrewarm;

    private GameObject main;
    private InclinedPanel.PanelType oldPanelType;


    private void OnEnable()
    {
        isGizmos = serializedObject.FindProperty("isGizmos");

        panelType = serializedObject.FindProperty("panelType");
        isPortalable = serializedObject.FindProperty("isPortalable");
        angle = serializedObject.FindProperty("angle");
        time = serializedObject.FindProperty("time");
        isPrewarm = serializedObject.FindProperty("isPrewarm");

        main = ((InclinedPanel)target).gameObject;
        oldPanelType = (InclinedPanel.PanelType)panelType.enumValueIndex;

        // ------------------------------------------
        if (PrefabUtility.IsAnyPrefabInstanceRoot(main))
        {
            PrefabUtility.UnpackPrefabInstance(main, PrefabUnpackMode.OutermostRoot, InteractionMode.AutomatedAction);
        }
    }


    public override void OnInspectorGUI()
    {
        GUI.enabled = false;
        EditorGUILayout.ObjectField("Script:", MonoScript.FromMonoBehaviour((InclinedPanel)target), typeof(InclinedPanel), false);
        GUI.enabled = true;

        serializedObject.Update();

        EditorGUILayout.PropertyField(isGizmos);
        EditorGUILayout.PropertyField(panelType);
        if(panelType.enumValueIndex != (int)InclinedPanel.PanelType.Glass)
            EditorGUILayout.PropertyField(isPortalable);
        EditorGUILayout.PropertyField(angle);
        EditorGUILayout.PropertyField(time);
        EditorGUILayout.PropertyField(isPrewarm);

        if (panelType.enumValueIndex != (int)oldPanelType)
            SetPanel((InclinedPanel.PanelType)panelType.enumValueIndex);

        oldPanelType = (InclinedPanel.PanelType)panelType.enumValueIndex;

        serializedObject.ApplyModifiedProperties();
    }


    private void SetPanel(InclinedPanel.PanelType panelType)
    {
        RemovePanel();

        string name = string.Empty;
        if (panelType == InclinedPanel.PanelType.Glass)
        {
            name = "InclinedPanel_Glass";
        }
        else // Tile
        {
            if (isPortalable.boolValue)
                name = "InclinedPanel_Tile_white";
            else
                name = "InclinedPanel_Tile_black";
        }

        string path = "Assets/Prefabs/Panels/InclinedPanel/Templates/" + name + ".prefab";
        Object temp = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
        GameObject prefab = (GameObject)PrefabUtility.InstantiatePrefab(temp);
        prefab.name = "Prefab";
        prefab.transform.parent = main.transform;
        prefab.transform.localPosition = Vector3.zero;
        prefab.transform.localRotation = Quaternion.identity;
    }
   

    private void RemovePanel()
    {
        Debug.Log("Remove");
        GameObject prefab = main.transform.Find("Prefab").gameObject;
        PrefabUtility.UnpackPrefabInstance(prefab, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
        DestroyImmediate(prefab);
    }
}


Wow! I managed to solve the problem, I just removed line 99 and now there are no errors.

1 Like

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 :0)
UnityEngine.InputSystem.Editor.PlayerInputEditor+<>c__DisplayClass6_0.b__0 () (at Library/PackageCache/com.unity.inputsystem@1.0.1/InputSystem/Plugins/PlayerInput/PlayerInputEditor.cs:285)
UnityEditor.EditorApplication.Internal_CallDelayFunctions () (at :0)

2 Likes

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”

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

4 Likes

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

3 Likes

I have the same problem, still haven’t solved it , followed all the steps from this tutorial.

I “fixed it” by not using the debug inspector.

8 Likes

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();

}

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.

2 Likes

This worked for me.
Thank you very much!

1 Like

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

3 Likes

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.

Thanks! This solves my problem!

This fixed it for me, what a silly bug!

I have the error and I need help on how to fix it. I have 2 scripts:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    Rigidbody2D rb;
    [Header("Speed Values")]
    public float speedX;
    public float speedY;
    [Header("Gravity")]
    public float gravityScale;
    float gravity;
    [Header("Public floats to be used in other scripts")]
    public float horizontal;

    // Start is called before the first frame update
    void Start()
    {   
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        gravity += gravityScale * Time.deltaTime;
        horizontal = Input.GetAxisRaw("Horizontal");
        if(Input.GetKeyDown(KeyCode.UpArrow)) {
            rb.velocity = new Vector2(rb.velocity.x, speedY);
        }
        Motion(horizontal);
        if(horizontal == 1) {
            transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
        } else if (horizontal == -1) {
            transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x) * -1, transform.localScale.y, transform.localScale.z);
        }
    }

    void Motion(float horizontal) {
        rb.velocity = new Vector2(horizontal * speedX, rb.velocity.y);
    }
}

and

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAnimator : MonoBehaviour
{
    Animator anim;
    PlayerController PlayerScript;
    // Start is called before the first frame update
    void Start()
    {
        PlayerScript = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        anim.SetFloat("sx", PlayerScript.horizontal);
    }   
}

Can you plz tell me how to fix this