Search Unity

ScriptableObject doesn't show enum in Inspector

Discussion in 'Scripting' started by b4gieta, Aug 12, 2019.

  1. b4gieta

    b4gieta

    Joined:
    Jul 17, 2018
    Posts:
    97
    Hi
    I've created script to chose type of mission but enum "Type" doesn't show in Inspector. What is the problem?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Playables;
    5.  
    6. [CreateAssetMenu(fileName = "New Mission", menuName = "Mission")]
    7. public class Mission : ScriptableObject
    8. {
    9.     public enum Type { KillEnemies, UseTrigger, Survive };
    10.  
    11.     public Type _type;
    12.  
    13.     private int SelectedType;
    14.  
    15.     // Use this for initialization
    16.     void Start()
    17.     {
    18.         switch (_type)
    19.         {
    20.             case Type.KillEnemies:
    21.                 SelectedType = 1;
    22.                 break;
    23.             case Type.UseTrigger:
    24.                 SelectedType = 2;
    25.                 break;
    26.             case Type.Survive:
    27.                 SelectedType = 3;
    28.                 break;
    29.         }
    30.     }
    31. }
    32.  
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Just add "[System.Serializable]" without the quotes before the "public enum" declaration.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    If I were in your position, I'd change the name "Type" to something else to test if that makes a difference. I don't know of any specific reason that it would, but "type" is the sort of word that could plausibly be a special case.

    Renaming your enum to something like "Goal" would probably make your code more understandable, anyway.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Tangentially, I notice it looks like you are using a switch statement to convert your enum into an integer. You are probably making this harder than necessary; enums are actually already integral data "under the covers", and you can explicitly specify what number corresponds to what value when you define the enum. Then you can convert back and forth between them using casts.

    Code (CSharp):
    1. public enum Goal
    2. {
    3.     KillEnemies = 1,
    4.     UseTrigger = 2,
    5.     Survive = 3
    6. }
    7.  
    8.  
    9. Goal goalAsEnum = Goal.KillEnemies;
    10. int goalAsInt = (int)goalAsEnum;     // goalAsInt == 1
    11. Goal anotherGoalAsEnum = (Goal)goalAsInt;    // anotherGoalAsEnum == Goal.KillEnemies
    In fact, when you are exposing your enum in the Unity inspector, it's almost always a good idea to specify the numeric values explicitly even if you don't care about them, because Unity only saves the number, not the name. If you edited your enum later in some way that changed the automatic numbering (by inserting or deleting some previous value), then your saved values wouldn't be the same "logical" value--they'd keep the same numerical value, even though that number is now assigned to a different enum value.