• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by WYSIWYGGG · Jul 08, 2020 at 12:38 AM · scripting problemenumscriptable object

Getting an "Enum" value from a Scriptable Object

Hi! I'm trying to "get" the value of an enum variable using a property getter named "VisumBlueprintChallengeMode" inside a scriptable object so I can "set" it to an "Enum" variable named "challenge".


Here's my code where the error exists (Look for "TODO: Fix Error")

 using System;
 using BaesickEntertainment.Scriptables;
 using UnityEngine;
 using Sirenix.OdinInspector;
 using Sirenix.Serialization;
 using Sirenix.Utilities;
 using Sirenix.Utilities.Editor;
 using UnityEditor;
 using UnityEngine.UIElements;
 
 namespace BaesickEntertainment.Editor.Consilio.Panels
 {
     
     /// <summary>
     /// Visum Creator is a level-creator editor where users can create their own "visum" (or stage).
     /// </summary>
     
     public class VisumCreator
     {
         // Cached References
         private VisumBlueprint _visumBlueprint;
         private VisumBlueprintEventSequence _visumBlueprintEventSequence;
         private EventSequenceEditor _eventSequenceEditor;
 
         #region Visum Blueprint Creator
         
         [PropertyOrder(1)]
         [BoxGroup("VISUM BLUEPRINT CREATOR")]
         public string visumBlueprintName = "visum blueprint 101";
         
         [PropertyOrder(1)]
         [BoxGroup("VISUM BLUEPRINT CREATOR")]
         [PropertyTooltip("unique visum blueprint identifier.")]
         [ReadOnly]
         public int visumBlueprintId = 0;
 
         [PropertyOrder(1)]
         [BoxGroup("VISUM BLUEPRINT CREATOR")]
         [Button(ButtonSizes.Large)]
         private void CreateNewVisumBlueprint()
         {
             _visumBlueprint = ScriptableObject.CreateInstance<VisumBlueprint>();
             
             // Create a new visum blueprint inside the project folder (temporarily).
             // TODO: Store the visum blueprint inside the Firebase to support user-generated levels
             AssetDatabase.CreateAsset(_visumBlueprint, "Assets/Commons/Scriptables/Visum Blueprints/" + visumBlueprintName + ".asset");
             AssetDatabase.SaveAssets();
             
             _visumBlueprint.VisumBlueprintName = visumBlueprintName;
             _visumBlueprint.VisumBlueprintId = visumBlueprintId;
             visumBlueprintId += 1;
         }
 
         #endregion
 
         [Space]
 
         #region Visum Blueprint Editor
         
         [PropertyOrder(2)]
         [Title("VISUM BLUEPRINT EDITOR")]
         [InfoBox("SELECT A VISUM BLUEPRINT TO EDIT", InfoMessageType.Warning, nameof(OnSetVisumBlueprint))]
         [AssetSelector]
         [OnValueChanged(nameof(CheckIfVisumBlueprintIsSet))]
         public VisumBlueprint visumBlueprint;
 
         // Invoke when a visum blueprint is set on the editpr
         private bool OnSetVisumBlueprint()
         {
             bool isVisumBlueprintNotSet;
 
             isVisumBlueprintNotSet = visumBlueprint == null;
 
             return isVisumBlueprintNotSet;
         }
 
         private bool IsVisumBlueprintSet()
         {
             bool isVisumBlueprintSet;
 
             isVisumBlueprintSet = visumBlueprint != null;
 
             return isVisumBlueprintSet;
         }
 
         #endregion
         
         #region Stage Details
 
         [EnableIf(nameof(IsVisumBlueprintSet), Value = true)] 
         [PropertyOrder(3)] 
         [Title("BASIC INFORMATION")]
         [LabelText("Blueprint Name")]
         public string blueprintName;
         
         [EnableIf(nameof(IsVisumBlueprintSet), Value = true)] 
         [PropertyOrder(3)] 
         [ReadOnly]
         [LabelText("Unique ID")]
         public int blueprintId;
         
         private void CheckIfVisumBlueprintIsSet()
         {
             // Load visual blueprint's editor values HERE
             if (visumBlueprint != null)
             {
                 blueprintName = visumBlueprint.VisumBlueprintName;
                 blueprintId = visumBlueprint.VisumBlueprintId;
                 blueprintDescription = visumBlueprint.VisumBlueprintDescription;
                 blueprintLoadingSplashImage = visumBlueprint.VisumBlueprintLoadingSplashImage;
                 // TODO: FIX THIS --->>> challenge = visumBlueprint.VisumBlueprintChallengeMode;
                 visumBlueprintEventSequence = visumBlueprint.VisumBlueprintEventSequence;
             }
             
             else
             {
                 blueprintName = String.Empty;
                 blueprintId = 0;
             }
         }
 
         [EnableIf(nameof(IsVisumBlueprintSet), Value = true)] 
         [PropertyOrder(3)]
         [LabelText("Description")]
         [PropertyTooltip("A short overview of what the blueprint is all about")]
         public string blueprintDescription;
     
         [Space(10)]
         
         [EnableIf(nameof(IsVisumBlueprintSet), Value = true)]
         [PropertyOrder(4)]
         [Title("MISCELLANEOUS")]
         [LabelText("Loading Splash Image")]
         [PropertyTooltip("The image to be shown in the loading screen before the stage start. (E.g: Trivia, Tip, Stage Detail)")]
         public Sprite blueprintLoadingSplashImage;
 
         #endregion
         
         [Space]
 
         #region Game Mode
         
         [EnableIf(nameof(IsVisumBlueprintSet), Value = true)]
         [PropertyOrder(5)]
         [Title("LEVEL CONFIGURATION")] 
         [InfoBox("CHOOSE A CHALLENGE", InfoMessageType.Error, "OnChallengeReferenceUpdate")]
         [InlineProperty, EnumPaging]
         [LabelText("Challenge Mode")]
         public Challenges challenge;
 
         public enum Challenges
         {
             None, Freestyle, TimeAttack, Survival
         }
 
         private bool OnChallengeReferenceUpdate()
         {
             bool isChallengeSetToNone;
         
             isChallengeSetToNone = challenge == Challenges.None;
 
             return isChallengeSetToNone;
         }
 
         #endregion
 
         #region Visum Blueprint Event Sequence
 
         [EnableIf(nameof(IsVisumBlueprintSet), Value = true)]
         [PropertyOrder(6)]
         [InlineButton(nameof(CreateNewVisumBlueprintEventSequence), "Create New Event Sequence")]
         [InlineButton(nameof(OpenVisumBlueprintEventSequenceEditor), "Edit")]
         public VisumBlueprintEventSequence visumBlueprintEventSequence;
 
         private void CreateNewVisumBlueprintEventSequence()
         {
             _visumBlueprintEventSequence = ScriptableObject.CreateInstance<VisumBlueprintEventSequence>();
             
             // Create a new visum blueprint inside the project folder (temporarily).
             // TODO: Store the visum blueprint inside the Firebase to support user-generated levels
             AssetDatabase.CreateAsset(_visumBlueprintEventSequence, "Assets/Commons/Scriptables/Events Sequence/" + blueprintName + "(Event Sequence).asset");
             AssetDatabase.SaveAssets();
             
             visumBlueprintEventSequence = _visumBlueprintEventSequence;
         }
 
         private void OpenVisumBlueprintEventSequenceEditor()
         {
             EventSequenceEditor.OpenWindow();
             _eventSequenceEditor.visumBlueprintEventSequence = visumBlueprintEventSequence;
         }
 
         #endregion
 
         #region Visum Blueprint Processors
 
         [EnableIf(nameof(IsVisumBlueprintSet), Value = true)]
         [PropertyOrder(6)]
         [HorizontalGroup("Visum Blueprint Processors")]
         [Button(ButtonSizes.Large)]
         [PropertySpace(SpaceAfter = 5)]
         private void SaveVisumBlueprint()
         {
             visumBlueprint.VisumBlueprintName = blueprintName;
             visumBlueprint.VisumBlueprintDescription = blueprintDescription;
             visumBlueprint.VisumBlueprintLoadingSplashImage = blueprintLoadingSplashImage;
             visumBlueprint.VisumBlueprintChallengeMode = challenge;
             visumBlueprint.VisumBlueprintEventSequence = visumBlueprintEventSequence;
             AssetDatabase.SaveAssets();
         }
         
         [EnableIf(nameof(IsVisumBlueprintSet), Value = true)]
         [PropertyOrder(6)]
         [HorizontalGroup("Visum Blueprint Processors")]
         [Button(ButtonSizes.Large)]
         [PropertySpace(SpaceAfter = 5)]
         private void ClearVisumBlueprintToDefault()
         {
             visumBlueprint.VisumBlueprintName = String.Empty;
             blueprintDescription = String.Empty;
             blueprintLoadingSplashImage = null;
             challenge = Challenges.None;
         }
 
         #endregion
     }
 }
 



Here's the script for my Scriptable Object:

 using System;
 using Sirenix.OdinInspector;
 using UnityEngine;
 
 namespace BaesickEntertainment.Scriptables
 {
     public class VisumBlueprint : ScriptableObject
     {
         #region Configuration Parameters
 
         private string _visumBlueprintName;
         private int _visumBlueprintId;
         private string _visumBlueprintDescription;
         private Sprite _visumBlueprintLoadingSplashImage;
         private Enum _visumBlueprintChallengeMode;
         private VisumBlueprintEventSequence _visumBlueprintEventSequence;
 
         #endregion
 
         #region Properties
         [Title("BASIC INFORMATION"), LabelWidth(150), LabelText("Name")]
         [ShowInInspector, ReadOnly]
         public string VisumBlueprintName
         {
             get => _visumBlueprintName;
             set => _visumBlueprintName = value;
         }
 
         [ShowInInspector, ReadOnly, LabelWidth(150), LabelText("Blueprint ID")]
         public int VisumBlueprintId
         {
             get => _visumBlueprintId;
             set => _visumBlueprintId = value;
         }
 
         [ShowInInspector, ReadOnly, LabelWidth(150), LabelText("Description")]
         public string VisumBlueprintDescription
         {
             get => _visumBlueprintDescription;
             set => _visumBlueprintDescription = value;
         }
 
         [Title("MISCELLANEOUS"), LabelWidth(150), LabelText("Loading Splash Image")]
         [ShowInInspector, ReadOnly]
         public Sprite VisumBlueprintLoadingSplashImage
         {
             get => _visumBlueprintLoadingSplashImage;
             set => _visumBlueprintLoadingSplashImage = value;
         }
 
         [Title("LEVEL CONFIGURATION")] 
         [ShowInInspector, ReadOnly, LabelWidth(150), LabelText("Challenge Mode")]
         public Enum VisumBlueprintChallengeMode
         {
             get => _visumBlueprintChallengeMode;
             set => _visumBlueprintChallengeMode = value;
         }
 
         [ShowInInspector, ReadOnly, LabelWidth(150), LabelText("Event Sequence")]
         public VisumBlueprintEventSequence VisumBlueprintEventSequence
         {
             get => _visumBlueprintEventSequence;
             set => _visumBlueprintEventSequence = value;
         }
 
         #endregion
     }
 }



Here's the error I'm getting: Assets\Scripts\Editor\Consilio\Panels\VisumCreator.cs(111,29): error CS0266: Cannot implicitly convert type 'System.Enum' to 'BaesickEntertainment.Editor.Consilio.Panels.VisumCreator.Challenges'. An explicit conversion exists (are you missing a cast?) alt text

untitled.png (12.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ChronoWalker · Jul 08, 2020 at 06:19 AM

You made VisumBlueprintChallengeMode an Enum, so variable "_visumBlueprintChallengeMode" should be declared as

private VisumBlueprintChallengeMode _visumBlueprintChallengeMode;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Lyrcaxis · Jul 08, 2020 at 05:07 AM

The image isn't playing, but I imagined you want something in this context:

 public enum Challenges { None, Freestyle, TimeAttack, Survival }
 
 // The base class object 
 public abstract class EnumWrapper<T> : ScriptableObject where T : System.Enum {
     public T value;
 }
 
 [CreateAssetMenu(menuName = "Blueprints/Challenge")]
 public class ChallengeVisumBlueprint : EnumWrapper<Challenges> {
     // Potential additional settings here
 }
 
 public class MyChallenge : MonoBehaviour {
 
     public ChallengeVisumBlueprint challenges;
 
     void Awake() {
         switch (challenges.value) {
             case Challenges.None:
                 break;
             case Challenges.Freestyle:
                 break;
             case Challenges.TimeAttack:
                 break;
             case Challenges.Survival:
                 break;
             default:
                 throw new ArgumentOutOfRangeException();
         }
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image WYSIWYGGG · Jul 08, 2020 at 05:18 AM 0
Share

I taught getting enum values on SO are similar from getting other values on types like string or in (like this: scriptableObjectName.variableYouWantToGet) but it won't let me: Assets\Scripts\Editor\Consilio\Panels\VisumCreator.cs(111,29): error CS0266: Cannot implicitly convert type 'System.Enum' to 'BaesickEntertainment.Editor.Consilio.Panels.VisumCreator.Challenges'. An explicit conversion exists (are you missing a cast?)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

238 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate ScriptableObject with a random asset from List 1 Answer

How to get data from Scriptable Objects without copy-pasting code 1 Answer

Use a enum from another script 2 Answers

Changing Enum var order messes up Enums saved in Scriptable Objects. 2 Answers

One Script Referred by Another Script used by Multiple Objects 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges