- Home /
 
Custom Editor Window not displaying anything
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.
Answer by FlaSh-G · Dec 29, 2017 at 07:45 PM
There's multiple things wrong with your editor script.
Why does the first editor line set the target as dirty? That isn't a thing you should do.
Why are you not using PropertyField with
serializedObject? It's way better than using these other methods withtarget. Check the link for a nice little example.What probably causes your problem: You're not calling
ApplyModifiedProperties. Again, check the link for an example.
Your answer
 
             Follow this Question
Related Questions
Get list of all "Action" classes 1 Answer
Stop a MonoBehavior from being addable as a script? 0 Answers
Distribute terrain in zones 3 Answers
Reorderable list of UnityEvents 2 Answers