Flag enum : byte throws InvalidCastException

Issue #781 resolved
LurkingNinja created an issue

Create a Flag enum field and select byte as an underlying type.

using System;
using UnityEngine;

public class Test : MonoBehaviour
{
        [Flags]
        private enum Bits : byte
        {
            B1 = 1 << 0,
            B2 = 1 << 1,
            B3 = 1 << 2,
            B4 = 1 << 3,
            B5 = 1 << 4,
            B6 = 1 << 5,
            B7 = 1 << 6,
            B8 = 1 << 7
        }

        [SerializeField] private Bits bits;
}

When you go to the inspector and try to edit the field, as soon as the dropdown opens the following exception is thrown:

InvalidCastException: Specified cast is not valid.
Sirenix.OdinInspector.Editor.EnumSelector1[T].BuildSelectionTree (Sirenix.OdinInspector.Editor.OdinMenuTree tree) (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/OdinSelectors/EnumSelector.cs:218) Sirenix.OdinInspector.Editor.OdinSelector1[T].get_SelectionTree () (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/OdinSelectors/OdinSelector.cs:198)
Sirenix.OdinInspector.Editor.OdinSelector1[T].SetupWindow (Sirenix.OdinInspector.Editor.OdinEditorWindow window, UnityEditor.EditorWindow prevSelectedWindow) (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/OdinSelectors/OdinSelector.cs:509) Sirenix.OdinInspector.Editor.OdinSelector1[T].ShowInPopup (UnityEngine.Vector2 position) (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/OdinSelectors/OdinSelector.cs:374)
Sirenix.OdinInspector.Editor.EnumSelector1[T].DrawEnumField (UnityEngine.GUIContent label, UnityEngine.GUIContent contentLabel, T value, UnityEngine.GUIStyle style) (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/OdinSelectors/EnumSelector.cs:473) Sirenix.OdinInspector.Editor.EnumSelector1[T].DrawEnumField (UnityEngine.GUIContent label, T value, UnityEngine.GUIStyle style) (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/OdinSelectors/EnumSelector.cs:512)
Sirenix.OdinInspector.Editor.Drawers.EnumDrawer1[T].DrawPropertyLayout (UnityEngine.GUIContent label) (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/Value Drawers/EnumDrawer.cs:34) Sirenix.OdinInspector.Editor.OdinDrawer.CallNextDrawer (UnityEngine.GUIContent label) (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/OdinDrawer.cs:155) Sirenix.OdinInspector.Editor.Drawers.PropertyContextMenuDrawer1[T].DrawPropertyLayout (UnityEngine.GUIContent label) (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/Misc Drawers/PropertyContextMenuDrawer.cs:662)
Sirenix.OdinInspector.Editor.OdinDrawer.DrawProperty (UnityEngine.GUIContent label) (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/OdinDrawer.cs:109)
Sirenix.OdinInspector.Editor.InspectorProperty.Draw (UnityEngine.GUIContent defaultLabel) (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/InspectorProperty.cs:807)
Rethrow as OdinPropertyException: This error occurred while being drawn by Odin.
Current IMGUI event: Used
Odin Property Path: bits
Odin Drawer Chain:

PropertyContextMenuDrawer<Test.Bits>
EnumDrawer<Test.Bits>
CompositeDrawer.
UnityEngine.Debug:LogException(Exception)
Sirenix.OdinInspector.Editor.InspectorProperty:Draw(GUIContent) (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/InspectorProperty.cs:884)
Sirenix.OdinInspector.Editor.InspectorProperty:Draw() (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/InspectorProperty.cs:695)
Sirenix.OdinInspector.Editor.Drawers.UnityObjectRootDrawer1:DrawPropertyLayout(GUIContent) (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/Value Drawers/UnityObjectRootDrawer.cs:60) Sirenix.OdinInspector.Editor.OdinDrawer:CallNextDrawer(GUIContent) (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/OdinDrawer.cs:155) Sirenix.OdinInspector.Editor.Drawers.FixBrokenUnityObjectWrapperDrawer1:DrawPropertyLayout(GUIContent) (at Assets/Plugins/Sirenix/Odin Inspector/Scripts/Editor/FixBrokenUnityObjectWrapperDrawer.cs:41)
Sirenix.OdinInspector.Editor.OdinDrawer:DrawProperty(GUIContent) (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/OdinDrawer.cs:109)
Sirenix.OdinInspector.Editor.InspectorProperty:Draw(GUIContent) (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/InspectorProperty.cs:807)
Sirenix.OdinInspector.Editor.PropertyTree:DrawProperties() (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/PropertyTree.cs:485)
Sirenix.OdinInspector.Editor.PropertyTree:Draw(Boolean) (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/Core/PropertyTree.cs:389)
Sirenix.OdinInspector.Editor.OdinEditor:DrawTree() (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/OdinEditor.cs:93)
Sirenix.OdinInspector.Editor.OdinEditor:DrawOdinInspector() (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/OdinEditor.cs:216)
Sirenix.OdinInspector.Editor.OdinEditor:OnInspectorGUI() (at Y:/Repositories/sirenix-development-framework/Sirenix Solution/Sirenix.OdinInspector.Editor/Drawers/OdinEditor.cs:85)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

With underlying int type, everything works fine.

My Odin is 3.0.5, but there is no option for it down below.

Unit Editor is 2021.1.6f1.

Comments (1)

  1. Tor Esa Vestergaard

    Thanks for the report - this bug has already been resolved and will be fixed by next patch. Meanwhile, if you'd like a build with it fixed, ping me on our Discord server and I will send you a build.

  2. Log in to comment