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

Question Save changes made in Custom Editor

Discussion in 'Editor & General Support' started by Kiryu144, May 23, 2020.

  1. Kiryu144

    Kiryu144

    Joined:
    Jul 31, 2013
    Posts:
    4
    I'm working on an inventory system. I have a slot gameobject for every slot I have in my inventory.
    Now I want to change the given item while in edit mode and have it in play mode aswell. Here is my current code.

    Code (CSharp):
    1.  
    2. [ExecuteInEditMode, SelectionBase]
    3. public class InventorySlot : MonoBehaviour
    4. {
    5.     [HideInInspector]
    6.     public Image spriteImage;
    7.  
    8.     [HideInInspector]
    9.     public Text amountText;
    10.  
    11.     private InventoryItem _item = null;
    12.     public InventoryItem item{ get { return _item; } set { _item = value; UpdateItem(); } }
    13.  
    14.     void Start()
    15.     {
    16.         UpdateItem();
    17.     }
    18.  
    19.     public void UpdateItem()
    20.     {
    21.         if(_item != null)
    22.         {
    23.             SetSprite(_item.sprite);
    24.             SetAmount(_item.amount);
    25.         }
    26.         else
    27.         {
    28.             SetSprite(null);
    29.             SetAmount(0);
    30.         }
    31.     }
    32.  
    33.     (...)
    34. }
    35.  
    Code (CSharp):
    1.  
    2. [CustomEditor(typeof(InventorySlot))]
    3. public class InventorySlotEditor : Editor
    4. {
    5.     public override void OnInspectorGUI()
    6.     {
    7.         base.OnInspectorGUI();
    8.  
    9.         InventorySlot inventorySlot = (InventorySlot)target;
    10.  
    11.         InventoryItem newItem = EditorGUILayout.ObjectField("Item", inventorySlot.item, typeof(InventoryItem), false) as InventoryItem;
    12.  
    13.         if(inventorySlot.item != newItem)
    14.         {
    15.             // Item changed
    16.             inventorySlot.item = newItem != null ? newItem.Clone() : null;
    17.         }
    18.  
    19.         if(inventorySlot.item != null)
    20.         {
    21.             int amount = EditorGUILayout.IntField("Amount", inventorySlot.item.amount);
    22.             if(amount != inventorySlot.item.amount)
    23.             {
    24.                 // Amount changed
    25.                 inventorySlot.item.amount = amount;
    26.                 inventorySlot.UpdateItem();
    27.             }
    28.         }
    29.     }
    30. }
    31.  
    The thing is, in edit mode everything works fine. But when I press play, my set item gets defaulted to null.
    How can I force unity to save my changes?

    Thanks.
     
  2. DiegoDePalacio

    DiegoDePalacio

    Unity Technologies

    Joined:
    Oct 28, 2009
    Posts:
    507
    Hi @antonberta,

    This is more likely happening because your `_item` variable is not being serialized and therefore not saved.

    Try to replace this line:
    private InventoryItem _item = null;

    with:
    Code (CSharp):
    1. [SerializeField]
    2. [HideInInspector]
    3. private InventoryItem _item = null;
    You can find more information about it here: https://docs.unity3d.com/ScriptReference/SerializeField.html


    Good luck with your Inventory System!