Unable to edit fields from custom inspector

Hey everyone,

I was trying to organize my item system via a custom inspector. I have never used custom editors before, and may have made an amateur mistake :frowning:

The script is making fields show up in the custom inspector, as expected, but when I try to edit the fields, they will no change. I will select a new value for my enum, but the inspector won’t assign it. I can’t assign game objects to my Object fields either and the float field is stuck at zero. Any advice is appreciated.

Here is the MonoBehaviour script that the editor script references:

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

public enum ItemClass{ Weapon, Armor, Consumable }

public class ItemIndex : MonoBehaviour
{

    
    public List<GameObject> item;
    public List<float> armor;
    public List<float> damage;
    public List<ItemClass> itemType;
}  

Here is the editor script that creates the faulty fields:

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

[CustomEditor(typeof(ItemIndex))]
public class ItemIndexEditor : Editor {

    public override void OnInspectorGUI()
    {
        ItemIndex index = (ItemIndex)target;
        
        int totalItems = 0;
        
        GameObject currentPrefab = null;
        float currentArmor = 0;
        float currentDamage = 0;
        ItemClass currentItemType = ItemClass.Weapon;
        
        currentPrefab = (GameObject)EditorGUILayout.ObjectField("Item Prefab", currentPrefab, typeof(GameObject), false);
        currentItemType = (ItemClass)EditorGUILayout.EnumPopup("Item Type", currentItemType);
        
        if(currentItemType == ItemClass.Weapon)
        {
            currentDamage = EditorGUILayout.FloatField("Damage", currentDamage);
        }
        else if(currentItemType == ItemClass.Armor)
        {
            currentArmor = EditorGUILayout.FloatField("Armor", currentArmor);
        }
        
        
        if(GUILayout.Button("Create Item"))
        {
            totalItems += 1;
            index.item.Add(currentPrefab);
            index.itemType.Add(currentItemType);
        }
        
        
    }
}

The editor script obviously isn’t finished, but I would like to fix the fields, before completing the rest of the script. Thanks for any help.

If memory serves me correctly, you should add:

        if(GUI.changed)
             EditorUtility.SetDirty(ItemValueChanged);   

In this case, it’d be things like: totalItems, etc.