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 Mattias-Wargren · Mar 01, 2011 at 08:54 AM · inspectorenum

Getting the selected enum value set in the Inspector

I cant seem to understand how I could use the inspector enum to set a variable in the Start function of my MonoBehaviour class.

// Class TypeList [System.Serializable] public class TypeList { public enum types { PLAYER, PROP, ENEMY }

 public const string PLAYER = "Player";
 public const string PROP = "Prop";
 public const string ENEMY = "Enemy";

};


// MonoBehaviour class public TypeList.types selectedType;

public void Start() { // How can I get the selected type from the variable selectedType? string selected = ?; }

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
2
Best Answer

Answer by taoa · Mar 01, 2011 at 09:10 AM

How about selectedType.ToString()?

Or, if you absolutely need to pick the string not from the name of the enum value but from some constant string, then assign an incrementing value to each member of your enum, and then create a constant string array where each member stores the string value of the corresponding enum value:

[System.Serializable] public class TypeList { public enum types { PLAYER = 0, PROP = 1, ENEMY = 2 }

 public const string[] typesStrings = {"Player", "Prop", "Enemy"};

};

// MonoBehaviour class public TypeList.types selectedType;

public void Start() { string selected = TypeList.typesStrings[(int)selectedType]; }

Comment
Add comment · Show 3 · 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 taoa · Mar 01, 2011 at 09:12 AM 0
Share

Or do you mean you want to pick the proper const string from the series you declared in your TypeList class?

avatar image Mattias-Wargren · Mar 01, 2011 at 09:17 AM 0
Share

Yes, I want to get the string seected ...

avatar image taoa · Mar 01, 2011 at 09:40 AM 1
Share

Wouldn't it be easier to name your enum values like you want them to look like as strings and use ToString()?

avatar image
1

Answer by Bovine · Mar 29, 2011 at 10:44 PM

You can also add attributes to your enum values such that you can get a string or other Value back from it as follows:

public class InfoValue : System.Attribute { private string _value;

 public InfoValue(string value)
 {
     _value = value;
 }

 public string Value
 {
     get { return _value; }
 }

 public static string GetInfoValue(Enum value)
 {
     string output = string.Empty;
     Type type = value.GetType();
     FieldInfo fi = type.GetField(value.ToString());
     InfoValue[] attrs = fi.GetCustomAttributes(typeof(InfoValue), false) as InfoValue[];
     if (attrs.Length > 0) output = attrs[0].Value;

     return output;
 }

}

And then do something like:

public enum types
{
    [InfoValue("Player")]
    PLAYER = 0,
    [InfoValue("Prop")]
    PROP = 1,
    [InfoValue("Enemy")]
    ENEMY = 2
}

And retrieve this with:

string my_string = InfoValue.GetInfoValue(types);

I am doing this a lot where I have data associated with an enum. In the context of my project, I am working on an RPG, so I have a bunch of statistics associated with each playable race and class and it makes sense to me to associate these with the enum value and then retrieve that.

However, I am doing this kind of thing VERY occasionally during Character Creation and not many times per frame and the code is reasonably heavy-weight compared to other options such as naming your enums such that ToString() can be used. You could also do some static construction of a Dictionary, but that's also heavy-weight.

If you're doing this in Start() methods, then I don't think it would be too bad, but I haven't exactly profiled it.

Hope that helps H

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 Bovine · Mar 29, 2011 at 10:47 PM 0
Share

If your doing things the other way around, you would want to do Enum.Parse() perhaps...

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

No one has followed this question yet.

Related Questions

Accessible collection of Type 1 Answer

Enum drop down menu in inspector for nested arrays 2 Answers

enum in inspector? 2 Answers

C# Edit enum values in inspector 1 Answer

Can I add an enum value in the inspector? 4 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