Editor GUILayout.Button restores changes once game starts

Hello there,

I am working on a puzzle game with a simple inventory system. The player has inventory slots which can contain a prefab and its available quantity. The player gets new items every level which are used to solve the puzzle.

I’ve tried coding an editor button which simply clears the inventory slot selected in the inspector. Everything is working well but once I run the game, it restores the previous values of the inventory slot, thus making the slot un-cleared and this button useless.

Here is my editor code:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(InventorySlot))]
public class InventorySlotEditor : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
       
        if (GUILayout.Button("Clear Slot"))
        {
            this.ClearSlot();
        }
    }

    private void ClearSlot()
    {
        InventorySlot slot = (InventorySlot)this.target;
        slot.itemPrefab = null;
        slot.itemCount = 0;
    }
}

I will thank everyone who may try to help.

Thanks a lot and have a great weekend!

You’re probably looking for this: