Issues with arrays on my custom editor

Hi there, not sure if this is the right place to ask this, but I’m having issues with a custom editor I’ve been working on. This is my first real custom editor and I can’t seem to get an array to change its members using EditorGUILayout.ObjectField. The values in the array keep resetting when I change them.

Here’s me editor code:

[CustomEditor(typeof(LootTable))]
public class LootTableEditor : Editor
{
    int labelSize = 40;
    int lootSize = 250;
    int oddsSize = 100;   
    int tableSize = 1;
    bool foldout = true;
    LootTable lootTable;

    ItemData[] itemDatas;
    float[] odds;

    private void OnEnable()
    {
        lootTable = (LootTable)target;
    }

    public override void OnInspectorGUI()
    {
        labelSize = EditorGUILayout.IntField("Label Size:", labelSize);
        lootSize = EditorGUILayout.IntField("Loot Field Size:", lootSize);
        oddsSize = EditorGUILayout.IntField("Odds Field Size:", oddsSize);
       

        tableSize = EditorGUILayout.IntField("Table Size:", tableSize);
        itemDatas = new ItemData[tableSize];
        odds = new float[tableSize];

        foldout = EditorGUILayout.Foldout(foldout, "Table");

        if (foldout)
        {
            for (int i = 0; i < tableSize; i++)
            {
                GUILayout.BeginHorizontal();

                EditorGUIUtility.labelWidth = labelSize;

// This is where the issue is

                itemDatas[i] = (ItemData) EditorGUILayout.ObjectField("Loot: ", itemDatas[i], typeof(ItemData), false, GUILayout.Width(lootSize));
                odds[i] = EditorGUILayout.FloatField("Odds: ", odds[i], GUILayout.Width(oddsSize));

                GUILayout.EndHorizontal();
            }
        }
    }

So I’m trying to add ItemData ScriptableObjects and floats to specify odds to this loot table but every time I change a value in the inspector it gets reset to null or 0 immediately.

I’m not having this issue with the int values like labelSize, only with the 2 values that are in the arrays.

My goal is to set these values in this inspector, then once they’re set, pass them onto the LootTable ScriptableObject (which this is the editor off) to set up the table.

Any help with this would be really appreciated!

You are trying to convert an ItemData to object and that does not work. Have you tried changing ItemData to object?

Just guessing since I have not seen the item data class.

[CustomEditor(typeof(LootTable))]
public class LootTableEditor : Editor
{
    private int labelSize = 40;
    private int lootSize = 250;
    private int oddsSize = 100;
    private int tableSize = 1;
    private bool foldout = true;
    private LootTable lootTable;

    private UnityEngine.Object[] itemDatas;
    private float[] odds;

    private void OnEnable()
    {
        lootTable = (LootTable) target;
    }

    public override void OnInspectorGUI()
    {
        labelSize = EditorGUILayout.IntField("Label Size:", labelSize);
        lootSize = EditorGUILayout.IntField("Loot Field Size:", lootSize);
        oddsSize = EditorGUILayout.IntField("Odds Field Size:", oddsSize);
        tableSize = EditorGUILayout.IntField("Table Size:", tableSize);

        itemDatas = new UnityEngine.Object[tableSize];
        odds = new float[tableSize];

        foldout = EditorGUILayout.Foldout(foldout, "Table");

        if (foldout)
        {
            for (int i = 0; i < tableSize; i++)
            {
                GUILayout.BeginHorizontal();

                EditorGUIUtility.labelWidth = labelSize;

// This is where the issue is
                itemDatas[i] =
                    EditorGUILayout.ObjectField(itemDatas[i], typeof(ItemData), false, GUILayout.Width(lootSize));
                odds[i] = EditorGUILayout.FloatField("Odds: ", odds[i], GUILayout.Width(oddsSize));

                GUILayout.EndHorizontal();
            }
        }
    }
}

@CurtisMcGill

I’ll give this a try as soon as I can, but that doesn’t explain why I’m having the same issue with odds _= EditorGUILayout.FloatField("Odds: ", odds*, GUILayout.Width(oddsSize));.*_
That’s just a float so I would think it should be working.

Sorry to be hard headed, but I’m a beginner. Do you mean no errors as in, it’s compiling? Because my code is compiling, it’s just that I can’t actually change the values of odds and ItemData.

But I can change the value of the other non-array elements. I’m using 2018.3.4f1 though, so I guess I could try to upgrade? I was scarred to do it mid-project haha

What version of Unity are you using?

In the script, I posted there are no errors in 2019. The odds are working because you are adding float array to float GUI layout.

The error is converting the ItemData to UnityEngine.Object. When you should be converting UnityEngine.Object to ItemData.

The UnityEngine.Object is a generic object and any item can be saved as Object. You tell the GUI what kind of Object you are passing with Type of.

I had to remove the “Loot” string as 2019 had no EditorGUILayout.ObjectField with string.

Do not upgrade the project, I will test in 2018.

What is the target? where is it set?

    private void OnEnable()
    {
        lootTable = (LootTable)target;
    }

So, update, I tried out the following changes:

ItemData itemData;
    float odd;

itemData = (ItemData)EditorGUILayout.ObjectField("Loot: ", itemData, typeof(ItemData), false, GUILayout.Width(lootSize));
        odd = EditorGUILayout.FloatField("Odds: ", odd, GUILayout.Width(oddsSize));

And that works fine. I can put in my ItemData objects and my floats just fine, so the issue is with the array elements:

int tableSize = 1;
ItemData[] itemDatas;
    float[] odds;
tableSize = EditorGUILayout.IntField("Table Size:", tableSize);
itemDatas = new ItemData[tableSize];
        odds = new float[tableSize];

for (int i = 0; i < tableSize; i++)
            {
                GUILayout.BeginHorizontal();

                EditorGUIUtility.labelWidth = labelSize;
                itemDatas[i] = (ItemData) EditorGUILayout.ObjectField("Loot: ", itemDatas[i], typeof(ItemData), false, GUILayout.Width(lootSize));
                odds[i] = EditorGUILayout.FloatField("Odds: ", odds[i], GUILayout.Width(oddsSize));

                GUILayout.EndHorizontal();
            }

For whatever reason, it doesn’t work on the array elements, but it does work if I just put in the elements one by one. I really need to get the array working though.

Another update:

I posted this question on Reddit and I figured out why the values were being reset based on the help there. The issue was this line:

itemDatas = new ItemData[tableSize];
        odds = new float[tableSize];

Makes sense, it was making a new array each time.

The issue now is that I can’t seem to figure out a way to directly manipulate the size of these arrays. I tried switching to Lists and still got nowhere.

I’m basically trying to recreate the default way lists and arrays work on the inspector, where you can type in a size, the array adjusts to that size, and then you can enter in values for each element. The key difference being that I’m trying to line up two elements from 2 arrays / lists.

I feel like this should be doable, but I can’t even figure out how to create that default functionality on a single array.