Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
3
Question by SolarChaser13 · Jul 24, 2017 at 08:50 PM · variableenumvalueinteger

How do I use an enum value as a variable?

I'm trying to make a weapon cycle through two modes, and in order to do this I've created an enum for the two modes, and tried to equal an integer to the length of the enum. However, apparently I can't use the enum as a variable. Since I have only beginner knowledge of enums, can someone fill me in?

Code:

 public enum PullModes {PullMode1, PullMode2};
 
     public PullModes pullModes;
 
     private int numberOfPullModes = System.Enum.GetValues(PullModes).Length;
Comment
Add comment · Show 1
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 jmgek · Jul 24, 2017 at 10:22 PM 0
Share
 using System;
 
 enum Priority
 {
     Zero,
     Low,
     $$anonymous$$edium,
     Important,
     Critical
 };
 
 class Program
 {
     static void $$anonymous$$ain()
     {
         // New local variable of the Priority enum type.
         Priority priority = Priority.Zero;
 
         // Set priority to critical on $$anonymous$$onday.
         if (DateTime.Today.DayOfWeek == DayOfWeek.$$anonymous$$onday)
         {
             priority = Priority.Critical;
         }
 
         // Write this if the priority is important.
         if (IsImportant(priority))
         {
             Console.WriteLine("The problem is important.");
         }
 
         // See if Low priority is important.
         priority = Priority.Low;
         Console.WriteLine(IsImportant(priority));
 
         // See if Important priority is.
         priority = Priority.Important;
         Console.WriteLine(IsImportant(priority));
     }
 
     static bool IsImportant(Priority priority)
     {
         // Switch on the Priority enum.
         switch (priority)
         {
             case Priority.Low:
             case Priority.$$anonymous$$edium:
             case Priority.Zero:
             default:
                 return false;
             case Priority.Important:
             case Priority.Critical:
                 return true;
         }
     }
 }

3 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by Bunny83 · Jul 24, 2017 at 10:25 PM

When you declare an enum you actually create a new type. However every enum type has an underlying integral data type (sbyte, byte, short, ushort, int, uint, long, ulong). If no explicit type is specifed the default is "int". So any enum value can be casted into it's underlying type and back:

 public enum MyEnum : int
 {
     A, B, C, D
 }
 
 // ...
 MyEnum someEnumValue;
 
 someEnumValue = MyEnum.B;
 int val = (int)someEnumValue; // val == 1
 someEnumValue = (MyEnum)2; // someEnumValue == "C"

Note that an enum value literally is the same as it's underlying value. You can even assign an integer value to an enum that doesn't have a corresponding "constant":

 someEnumValue = (MyEnum)42; // no constant inside MyEnum for this value 42

A common way if you want a countable enum is to define a special "Count" or "Size" value as last element:

 public enum Mode
 {
     M1,     // == 0
     M2,     // == 1
     M3,     // == 2
     M4,     // == 3
     Count   // == 4
 }
 
 public Mode mode;

This way one can simply do:

 mode++;
 if (mode >= Mode.Count)
     mode = Mode.M1;

This would increase the value by one each time and checks if we reached our additional "Count" value if so go back to the first. That way you have a cycle. M1 -> M2 .> M3 -> M4 -> M1 -> M2 -> ...

Note that this of course only works as long as your enum values are sequencial. Enum values can be specified however you like:

 public MyEnum
 {
     A = 45,
     B = 545,
     D = 65536,
     chicken = 1
 }


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
1

Answer by tomandresen · Jul 24, 2017 at 09:09 PM

Am I correct in assuming you don't actually want to assign the length of the enum (which in the above example will always be 2)?

If you want to switch between the two modes, this is how to do it:

     public void Start()
     {
         // Default to first pullmode
         public PullModes pullmodes = Pullmodes.PullMode1;
     }

     public void Update()
     {
         SwapPullModes();
     }
     
     private void SwapPullModes ()
     {
         // Switch between the two pull modes
         if (pullModes == PullModes.PullMode1)
         {
             pullModes = PullModes.PullMode2;   
         }
         else
         {
             pullModes = PullModes.PullMode1;
         {
     
     }





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 SolarChaser13 · Jul 24, 2017 at 09:24 PM 0
Share

@tomandresen

This works fine. I do have one question, though: what would I do if I had more than 2 modes to the weapon?

avatar image
0

Answer by c4ctus · Jul 24, 2017 at 10:02 PM

If you want to move the enum to the next value you can use this

 public static class EnumExtensions
 {
     public static T Next<T>(this Enum e)
     {
         try
         {
             var enumInfo = Enum.GetValues(typeof(T));
             var currentValue = e.ToString();
             for (int a = 0; a < enumInfo.Length; a++)
             {
                 if (((T)enumInfo.GetValue(a)).ToString() == currentValue.ToString())
                 {
                     if (a == enumInfo.Length - 1)
                     {
                         return (T)enumInfo.GetValue(0);
                     }
                     else
                     {
                         return (T)enumInfo.GetValue(a + 1);
                     }
                 }
             }
         }
         catch (Exception ex)
         {
             Debug.Log("Error while read enum: " + ex.Message);
         }
         return default(T);
     }
 }
 
 public class YourClass : MonoBehaviour
 {
     public enum PullModes { PullMode1, PullMode2, PullMode3 };
 
     public PullModes pullModes;
 
     public void Awake()
     {
         for (int i = 0; i < 10; i++)
         {
             pullModes = pullModes.Next<PullModes>();
             Debug.Log(pullModes.ToString());
         }
     }
 }
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

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.

Follow this Question

Answers Answers and Comments

76 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

Related Questions

Use enum value as a index variable of a list 1 Answer

Save Integer Values 1 Answer

Divisible by 3 2 Answers

Flag structure variable in unity editor 1 Answer

Variable won't update within a certain function 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges