- Home /
UnityEvent Editor breaking in nested struct
We are using UnityEvent for our in-game menu but we ran into some issues when trying to use it in a nested struct for sub menus. It works fine in the top level struct but breaks in the sub level structs.
public class TestScript : MonoBehaviour
{
[Serializable]
public struct menuItem
{
public UnityEvent menuEvent;
public menuItem[] subMenu;
}
public menuItem[] menuItems;
}
It throws the following error
type is not a enum value UnityEditor.SerializedProperty:get_enumValueIndex() UnityEditorInternal.UnityEventDrawer:GetMode(SerializedProperty) (at C:/buildslave/unity/build/Editor/Mono/Inspector/UnityEventDrawer.cs:161) UnityEditorInternal.UnityEventDrawer:DrawEventListener(Rect, Int32, Boolean, Boolean) (at C:/buildslave/unity/build/Editor/Mono/Inspector/UnityEventDrawer.cs:196) UnityEditorInternal.ReorderableList:DoListElements(Rect) (at C:/buildslave/unity/build/Editor/Mono/GUI/ReorderableList.cs:576) UnityEditorInternal.ReorderableList:DoList(Rect) (at C:/buildslave/unity/build/Editor/Mono/GUI/ReorderableList.cs:401) UnityEditorInternal.UnityEventDrawer:OnGUI(Rect) (at C:/buildslave/unity/build/Editor/Mono/Inspector/UnityEventDrawer.cs:146) UnityEditorInternal.UnityEventDrawer:OnGUI(Rect, SerializedProperty, GUIContent) (at C:/buildslave/unity/build/Editor/Mono/Inspector/UnityEventDrawer.cs:115) UnityEditor.DockArea:OnGUI() 
Is this a known issue and if so does anyone know how to fix this or know of a work around?
Answer by Bunny83 · Apr 13, 2017 at 10:14 AM
First of all i wouldn't recomment using a struct here. Even Unity now supports structs it usually gives you more headache than it does any good.
Next thing is, unfortunately, you can't have a recursive structure, at all. "menuItem" has "menuItem"s as childs. The Unity serializer don't like this. The max overall serialization depth is only about 7 i think. However recursive structures will trigger a safety switch which doesn't allow such structures at all even when the depth isn't actually reached.
Finally the UnityEvent class already has several childs. The UnityEventBase class (the base of UnityEvent) has a "PersistentCallGroup" class as child. The PersistentCallGroup class has a List of "PersistentCall" classes. The PersistentCall has an "ArgumentCache" class as child. So a UnityEvent already has about 3 depth layers internally so yo uhave to be careful how deep you nest a UnityEvent.
In your particular case the problem most likely comes from the recursion. If you need a structure like that, you have to use ScriptableObjects which can be serialized on their own and can be cross referenced.
Your answer
Follow this Question
Related Questions
Get mouse position in Editor based on screen coordinates 1 Answer
The type or namespace name 'EventSystems' does not exist in the namespace 'UnityEngine' 0 Answers
Mouse click in edit mode. ✱✺✸❁ 1 Answer
UnityEvent draws debug variables in the inspector 0 Answers
ShowObjectPicker ObjectSelectorClosed command is never issued 1 Answer