Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Herve_Simard · Apr 22, 2012 at 03:21 PM · c#functionstringconvertenum

Cast the result of an enum as a string

Hi everyone,

I've got this function working as an Instantiate, meaning that it needs a string, a position and a rotation. Using a system I made, I can implement the position and the rotation, but I've got troubles with the string.

I need to call on something using the result of an enum. So I've got this enum:

public enum enumType {ONE, TWO, THREE, FOUR};

public enumType type;

But since it's giving me an int, I cannot convert this to a string. The enum is important because it allows me to set things appropriately.

Is there any way that I could "interpret" the result of the enum so that I could send a correct string to my function?

NOTE : I'm coding in c#.

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 Owen-Reynolds · Apr 22, 2012 at 04:43 PM 0
Share

??? Instantiate uses GameObjects or Transforms -- they don't need strings. And the purpose of an enumerated type is to make a working program easier to read, by turning 1,2,3 into meaningful words, like COW, DOG, LION.

I think you're halfway down a bad road to solve whatever your original problem was.

avatar image Herve_Simard · Apr 22, 2012 at 08:40 PM 0
Share

I understand your comment, and I want to say that you're not wrong. It's just that I'm using a singleton that instantiates all my prefab at the beginning of the game. To get them back, I need to use a string to get them, but I have this enum problem. See what I mean?

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by GutoThomas · Apr 22, 2012 at 03:27 PM

Have you already tried explicit conversion?

Like

 (string)type

or

 type as string

?

In my case, if I ain't wrong, the result is always passed like some sort of string. It only works as an int if a do an explicit conversion, like the one I show before.

Comment
Add comment · Show 2 · 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 Bunny83 · Apr 23, 2012 at 05:43 PM 2
Share

And like any type in .NET you could also use the ToString function explicitly or implicitly:

 public enumType type = enumType.TWO;
 string enemy = type.ToString();       // explicit
 // or
 string enemy = "" + type;             // implicit
avatar image CoalCzar · Jul 20, 2013 at 10:14 PM 0
Share
 (string)type
 
 &&
 
 type as string

did not work, but .ToString() did!

avatar image
0

Answer by AchillesWF · Apr 22, 2012 at 03:25 PM

Well, interestingly in your enum ONE is actually zero, TWO is one, and so on. So even if you just used .ToString() on the int representation of the enumeration value, you will not technically get the correct results. To change this, make your enum like this:

public enum enumType { ONE = 1, TWO, THREE, FOUR }

int values should have a ToString() function available in C# that gives you the string representation of the number.

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 Herve_Simard · Apr 23, 2012 at 04:51 PM

I have my answer (finally!).

Here:

string enemy = Enum.GetName(typeof(Wave.enemyType), waves[currentWave].type); where Wave.enemyType is my enum, and waves.type is the variable of my enum.

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 Bunny83 · Apr 23, 2012 at 05:48 PM 1
Share

That's a bit complicated to just get the enums value as string. José Augusto $$anonymous$$ showed you an easier way.

btw. It's common practise to start type names with a capital letter: "EnumType" so they don't get confused with valiable names like "currentWave".

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

7 People are following this question.

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

Related Questions

How to convert a list of Resolution to String 2 Answers

What is this C# code in javascript 0 Answers

Most efficient way to convert string[] to int [] in C#? 2 Answers

Convert Text to float 3 Answers

Convert Int 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