Hello Everyone!
I’ve been trying recently to implement a ScriptableObject in my game that will store all items pertaining to a certain type. That way, whenever I want to make a crafting recipe (in a separate code), I can just go to the respective asset and search it’s list of items.
My ItemGroup (what I decided to call this ScriptableObject) script looks like this:
[CreateAssetMenu(fileName = "New Item Group", menuName = "Item Group", order = 0)]
public class ItemGroup : ScriptableObject
{
public int type;
public List<Item> itemGroup = new List<Item>();
Item SearchByID(int searchId)
{
Item tempItem = null;
foreach (Item item in itemGroup)
{
if (item.id == searchId)
{
tempItem = item;
}
}
if (tempItem != null)
{
return tempItem;
} else
{
Debug.LogError("No Item with ID " + searchId + " found in ItemGroup!");
return null;
}
}
}
The ItemGroupViewer Script looks like this:
[CustomEditor(typeof(ItemGroup))]
public class ItemGroupViewer : Editor {
Vector2 scroll = new Vector2();
public override void OnInspectorGUI()
{
EditorUtility.SetDirty(target);
//base.OnInspectorGUI();
ItemGroup ig = (ItemGroup)target;
EditorGUILayout.BeginVertical();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Item Type:");
ItemType selected = (ItemType)ig.type;
ig.type = (int)(ItemType)EditorGUILayout.EnumPopup(selected);
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
scroll = EditorGUILayout.BeginScrollView(scroll, GUILayout.Height(250f));
foreach(Item item in ig.itemGroup)
{
ItemField(item);
}
EditorGUILayout.EndScrollView();
if (GUILayout.Button("Add New Item"))
{
CreateNewItem(ig);
Debug.Log(ig.itemGroup.Count);
}
EditorGUILayout.EndVertical();
}
}
Everything is working fine and dandy, because it is displaying everything properly at this point. But when I close Unity and reopen it, the Inspector does not display anything at all for the ItemGroup assets, not even the file names. It only comes back when I change something in the ItemGroupViewer script, forcing Unity to reload the Inspector.
Is there a way to avoid this? Going to the script and changing it is time consuming and annoying. Besides, sometimes not even changing it works, forcing me to create a new ItemGroup and fill it with all the item names again!
PS: If it is important, the Item class present in the ItemGroup script is marked as Serializable.