Let's say we have the following Enums
public enum Section
{
A,
B
}
public enum SectionA
{
A1,
A2,
A3
}
public enum SectionB
{
B1,
B2,
B3
}
I would like to make a script with two public Enum fields, with selectable values as dropdowns from the Unity Editor. The first one serves to select the Section (A or B), while the second one should be an Enum of type SectionA or SectionB depending what the selected value on the first field is.
I made the following scripts for this:
public class Item : MonoBehaviour
{
[HideInInspector]
public Section Section;
[HideInInspector]
public System.Enum Value;
}
[CustomEditor(typeof(Item))]
public class ItemEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
Item item = target as Item;
item.Section = (Section)EditorGUILayout.EnumPopup(item.Section);
switch (item.Section)
{
case Section.A:
item.Value = (SectionA)EditorGUILayout.EnumPopup(item.Value);
break;
case Section.B:
item.Value = (SectionB)EditorGUILayout.EnumPopup(item.Value);
break;
}
}
}
But it triggers an Exception stating that item.Value is null.
I also tried replacing
item.Value = (SectionA)EditorGUILayout.EnumPopup(item.Value);
with
item.Value = (SectionA)EditorGUILayout.EnumPopup(item.Value ?? SectionA.A1);
to try to give it an "Initial Value" but then the value A1 overrides the selected one when I press Play. Any ideas?