- Home /
Custom Inspector on scriptable object not displaying list elements until after I update script
I'm trying to make a type of scriptable object that acts as a 'base' for items to be generated with in my game. For the sake of keeping everything nicely tied together, I wanted to manage the creation of different types of items with one 'item' class. Basically have an enum change what parameters display in the inspector. This was all working fine when the itemBase class inherited from monodevelop, but once I changed it to a scriptable object so I could have different item configurations saved I started having issues. The 'mods' List is now giving me nullReferenceExceptions when I'm trying to reference it in my custom editor. Not sure why?
Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[System.Serializable]
[CreateAssetMenu(fileName = "item base", menuName = "Items/itemBase", order = 1)]
public class itemBase : ScriptableObject
{
public enum Rarity { basic, uncommon, rare, artifact }
public enum ItemType { weapon, armour, currency }
public string itemName;
public string description;
public Rarity rarity;
public ItemType itemType;
//equipment (weapons/armour) only
public bool canEquip;
public int levelReq;
public int carryWeight;
public List<mod> mods;
public int modCount;
//weapons only
public int minDmg, maxDmg;
//armour only
public int defence;
//currency only
public int quantity;
}
#if UNITY_EDITOR
[CustomEditor(typeof(itemBase))]
public class itemInspectorEditor : Editor
{
public override void OnInspectorGUI()
{
itemBase enumScript = target as itemBase;
var modsList = enumScript.mods;
//Enum drop down
enumScript.itemType = (itemBase.ItemType)EditorGUILayout.EnumPopup(enumScript.itemType);
//default variables for all items
enumScript.itemName = EditorGUILayout.TextField("Item Name", enumScript.itemName);
enumScript.description = EditorGUILayout.TextField("Description", enumScript.description);
enumScript.rarity = (itemBase.Rarity)EditorGUILayout.EnumPopup("Rarity", enumScript.rarity);
switch (enumScript.itemType)
{
case itemBase.ItemType.weapon:
enumScript.carryWeight = EditorGUILayout.IntField("Carry Weight", enumScript.carryWeight);
enumScript.levelReq = EditorGUILayout.IntField("Level Requirement", enumScript.levelReq);
enumScript.minDmg = EditorGUILayout.IntField("min Damage", enumScript.minDmg);
enumScript.maxDmg = EditorGUILayout.IntField("max Damage", enumScript.maxDmg);
enumScript.modCount = Mathf.Max(0, EditorGUILayout.IntField("Possible Mods", enumScript.modCount));
while (enumScript.modCount < modsList.Count) //this is where I get the nullReferenceException
{
modsList.RemoveAt(modsList.Count - 1);
}
while (enumScript.modCount > modsList.Count)
{
modsList.Add(null);
}
for (int i = 0; i < modsList.Count; i++)
{
modsList[i] = (mod)EditorGUILayout.ObjectField(modsList[i], typeof(mod), false);
}
break;
case itemBase.ItemType.armour:
enumScript.carryWeight = EditorGUILayout.IntField("Carry Weight", enumScript.carryWeight);
enumScript.levelReq = EditorGUILayout.IntField("Level Requirement", enumScript.levelReq);
enumScript.defence = EditorGUILayout.IntField("Defence", enumScript.defence);
enumScript.modCount = Mathf.Max(0, EditorGUILayout.IntField("Possible Mods", enumScript.modCount));
while (enumScript.modCount < modsList.Count)
{
modsList.RemoveAt(modsList.Count - 1);
}
while (enumScript.modCount > modsList.Count)
{
modsList.Add(null);
}
for (int i = 0; i < modsList.Count; i++)
{
modsList[i] = (mod)EditorGUILayout.ObjectField(modsList[i], typeof(mod), false);
}
break;
case itemBase.ItemType.currency:
enumScript.quantity = EditorGUILayout.IntField("Quantity", enumScript.quantity);
break;
}
}
}
#endif
Your answer
Follow this Question
Related Questions
CustomEditor for an ScriptableObject asset only works after recompile. 1 Answer
Draw scriptable object "inspector" in custom window (EditorWindow) 1 Answer
Why isn't my CustomEditor's OnInspectorGUI() running? 1 Answer
How to create custom editor for a class contained within another class 3 Answers
"Type mismatch" in Inspector for GameObject type in asset file 2 Answers