Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
6
Question by Ignacio 1 · Mar 14, 2011 at 12:56 PM · stringenum

Converting a string to an enum

Hi! I need to convert a string into its equivalent enum object. Although I could make an auxiliary function to do that, I wonder if C# can do that for me and I've found this article, but I'm afraid it doesn't work in Unity.

I've been searching here through Unity Answers but haven't been able to find a solution, can you help me?

Thanks in advance!

Comment
Add comment · Show 2
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 SirGive · Mar 14, 2011 at 01:01 PM 0
Share

That does work. Let me see if I can find an example for you.

avatar image Ignacio 1 · Mar 14, 2011 at 01:33 PM 0
Share

Yes, sorry about being a little bit vague. I should have mentioned I was getting a compilation error because I was trying to use it exactly as it appeared in the article (i.e., by using Enum.Parse). I tried the solution that taoa suggested and it worked! Thanks for your reply!

2 Replies

· Add your reply
  • Sort: 
avatar image
36
Best Answer

Answer by taoa · Mar 14, 2011 at 01:09 PM

 YourEnumType parsed_enum = (YourEnumType)System.Enum.Parse( typeof(YourEnumType), your_string );


This works!

Comment
Add comment · Show 10 · 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 Ignacio 1 · Mar 14, 2011 at 01:32 PM 0
Share

Great! $$anonymous$$any thanks!

avatar image Anxo · Jan 30, 2013 at 03:57 AM 0
Share

where does the string go?

avatar image Zarenityx · Jun 03, 2013 at 06:36 PM 0
Share

@Anxowtf

The initial string goes into the 'your_string' variable in the example code above. The function then looks for an entry in the specified enumeration that matches the given string and returns it.
Imagine the opposite of enum.ToString().

avatar image plingativator · Oct 15, 2014 at 10:53 PM 16
Share

I found I also needed to convert as in the code below:

YourEnumType parsed_enum = (YourEnumType)System.Enum.Parse( typeof( YourEnumType ), your_string );

avatar image luislodosm · Mar 07, 2017 at 10:10 PM 5
Share

I got an error. This fix it:

 YourEnumType yourEnum = (YourEnumType) System.Enum.Parse (typeof (YourEnumType), yourString);
avatar image Necrohunter luislodosm · Mar 30, 2020 at 10:11 AM 2
Share

hmmm correct me if im wrong, but this is the exact same code as he wrote

Show more comments
avatar image
2

Answer by Pro-senik · Feb 21, 2019 at 06:31 PM

Another option with detecting parse error would be:

 if( System.Enum.TryParse<YourEnumType>(yourString, out YourEnumType yourEnum) )
 {
         ... if string was correctly parsed, you can now use yourEnum variable
 }
Comment
Add comment · Show 6 · 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 Bonfire-Boy · Feb 21, 2019 at 10:41 PM 0
Share

You've written the function call (partly) like a declaration. It should be

 YourEnumType yourEnum;
 if( System.Enum.TryParse<YourEnumType>(yourString, out yourEnum) )
 {
 }
avatar image Pro-senik Bonfire-Boy · Feb 22, 2019 at 12:57 AM 1
Share

As of C# 7.0 the ability to declare a variable right at the point where it is passed as an out argument was introduced.

avatar image Bonfire-Boy Pro-senik · Feb 22, 2019 at 08:52 AM 0
Share

Apologies, I didn't know that, that's a good development. How do I go about using it in Unity? Do I need to move to .Net 4 or something? I see that 3.5's on the way out so I guess that's a good idea anyway

Show more comments
avatar image Bunny83 · Feb 25, 2019 at 11:57 AM 0
Share

Shouldn't it also work like this:

 if( System.Enum.TryParse(yourString, out YourEnumType yourEnum) )

Or did the adhoc variable declaration break the generic type inference features of the compiler? ^^

avatar image Pro-senik Bunny83 · Feb 25, 2019 at 01:08 PM 0
Share

You are correct, it can be simplified like this. I just wanted to implicitly show the whole struct of the method for better understanding.

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

12 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

Related Questions

How would you convert string to enum 1 Answer

String Assets? 1 Answer

Parsing String from script A into Enum in script B 1 Answer

Cast the result of an enum as a string 3 Answers

Converting enum to string ? 1 Answer


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