Search Unity

enum index is out of range - Strange Issue With Custom Editor

Discussion in 'Scripting' started by karljj1, Sep 13, 2012.

  1. karljj1

    karljj1

    Joined:
    Feb 17, 2011
    Posts:
    440
    Hi I have the following Enum:

    Code (csharp):
    1.  
    2.     public enum ProtocolFamily : byte
    3.     {
    4.         Other                                               = 0,
    5.         EntityInformationInteraction                        = 1,
    6.         Warfare                                             = 2,
    7.         Logistics                                           = 3,
    8.         RadioCommunications                                 = 4,
    9.         SimulationManagement                                = 5,
    10.         DistributedEmissionRegeneration                     = 6,
    11.         EntityManagement                                    = 7,
    12.         Minefield                                           = 8,
    13.         SyntheticEnvironment                                = 9,
    14.         SimulationManagementwithReliability                 = 10,
    15.         LiveEntity                                          = 11,
    16.         NonRealTime                                         = 12,
    17.         InformationOperations                               = 13,
    18.         ExperimentalCGF                                     = 129,
    19.         ExperimentalEntityInfomationFieldInstrumentation    = 130,
    20.         ExperimentalWarfareFieldInstrumentation             = 131,
    21.         ExperimentalEnviromentObjectInfomationInteraction   = 132,
    22.         ExperimentalEntityManagement                        = 133
    23.     }
    24.  
    and this in a custom editor:

    Code (csharp):
    1.  
    2. // ProtocolFamily            
    3.             protocolFamily.enumValueIndex = ( int )( ProtocolFamily )EditorGUILayout.EnumPopup( protocolFamilyLabel, ( ProtocolFamily )protocolFamily.enumValueIndex );
    4.  
    When I select ExperimentalCGF or above I get the error 'enum index is out of range'. Having experimented a bit it seems that if I set ExperimentalCGF to 18 it will work but 19 and above cause the error. It seems Unity is validating this value by assuming they are all numbered in order, e.g 18 items in my enum so if the value is above 18 then its BAD!!!
    However this does not happen if i allow the default inspector view. The problem seems to be the SerializedProperty class (protocolFamily.enumValueIndex).

    Does anyone know how I can fix this or get around it without using the default inspector?

    Thanks

    Karl


    Edit:

    Doh!
    Of course its treating the enum values as an array, so 18 values!.
    Fixed it by using

    Code (csharp):
    1.  
    2. protocolFamily.intValue = ( int )( ProtocolFamily )EditorGUILayout.EnumPopup( protocolFamilyLabel, ( ProtocolFamily )protocolFamily.intValue );
    3.  
     
    Last edited: Sep 13, 2012
  2. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    I know C# is a lot more exacting in types, but what's wrong with:

    Code (csharp):
    1. protocolFamily = (ProtocolFamily) EditorGUILayout.EnumPopup( protocolFamilyLabel, (System.Enum) protocolFamily );
    System.Enum is hopefully unnecessary, but *shrug*.

    Edit: Yeah, System.Enum seems to be unnecessary.
     
    Last edited: Sep 13, 2012
  3. karljj1

    karljj1

    Joined:
    Feb 17, 2011
    Posts:
    440
    Hi Louis. using serialised properties allows for changes to be recorded so you can do undo and redo operations. It's a nicer way of doing things than manually calling undo.register yourself.

    Karl
     
  4. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    Ah, I missed the part where you mentioned you were using SP (saw it now, derp). Glad you found a solution.

    On a side note, is your undo working correctly? Even in very simple test classes, I can only undo to even-numbered steps.
     
  5. karljj1

    karljj1

    Joined:
    Feb 17, 2011
    Posts:
    440
    What do you mean even numbered steps? It seems to be working. I have spent ages.getting it too work lol

    Karl