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 CDK · May 08, 2013 at 07:34 AM · javascript

Enum ToString via Int (JS)

 for (var L in lines)
            {
                var Done1 : boolean = false;
             var S = L.Substring(5).Split(";"[0]);
             ImportedItems.Add(ImportedItems[0]);
             var values = S[0].Replace(" ","").Split(", "[0]);
             ImportedItems[x].Name = values[0];
             ImportedItems[x].Desc = values[1];
             ImportedItems[x].Price = float.Parse(values[2]);
             Debug.Log("String version of Type: " + values[3]);
             /*if(values[3] == "Loot")
             {
                 ImportedItems[x].Type = 1;
             }*/
             for(var x = 0; x < 10 && !Done1; x++)
             {    
                 if(float.Parse(values[3]) == Type[ x ])
                 {
                     ImportedItems[x].Type = float.Parse(values[3]);
                     Done1 = true;
                 }
             }
             if(x == 10 && !Done1)
                 Debug.LogError("Error: Item Type fail!");   

I have this piece of code and need to know how to fix the if statement... "Type.x.ToString("F")) obiously doesn't work. So how do i take the x value of the enum, and make it a string? Anyone? Inside "Type" there is:

 enum Type { Loot = 1, Food = 2, Weapon = 3, Hat = 4, Shoulders = 5, Gloves = 6, 
 Body = 7, Pants = 8, Boots = 9, Coins = 10, none }


Inside the text asset value is taken out of is the following:

 Item: Sword, An old rusty Sword, 15, Weapon;
 Item: Axe, An old rusty Axe, 10, Weapon;
 Item: Toast, Tasty apple pie, 500, Food;
 Item: Battery, Metallica and metal, 5, Loot;

The number values in the enum was a way to possibly fix it by putting a float inside the textasset but i still needed to acesss Type with 'x'.

Thanks in advance.

-CDK

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 CDK · May 08, 2013 at 07:21 AM 0
Share

By the way the following error comes with the line when i do "Type[x]": (43,43): BCE0048: Type 'System.Type' does not support slicing.

avatar image Graham-Dunnett ♦♦ · May 08, 2013 at 07:37 AM 1
Share

Using an enum called Type is going to get confusing for the compiler. Call it $$anonymous$$yType or something other than Type.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Graham-Dunnett · May 08, 2013 at 07:42 AM

I believe your question is:

"How can I convert a string to an enum of the same name?

 enum MyType { Loot = 1, Food = 2, Weapon = 3, Hat = 4, Shoulders = 5, Gloves = 6, 
 Body = 7, Pants = 8, Boots = 9, Coins = 10, none };

 MyType fred = ConvertFunction("weapon");

How should I write ConvertFunction?"

In which case just have a big if-else if statement that maps the strings to the enum values.

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 CDK · May 08, 2013 at 07:55 AM 0
Share

There have to be a better way than ALOT of if(values[3] == "Loot") { ImportedItems[x].Type = 1; } EDIT: If there isn't i guess i have to do it.. But it just doesn't sound right.

avatar image
0

Answer by DaveA · May 08, 2013 at 07:51 AM

See http://msdn.microsoft.com/en-us/library/system.enum.tostring.aspx and http://msdn.microsoft.com/en-us/library/system.enum.getvalues.aspx and http://msdn.microsoft.com/en-us/library/system.enum.tryparse.aspx

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 CDK · May 08, 2013 at 08:30 AM 0
Share

Not to be an ass but that's all answers if i was using C#... Which im not using.. Which i said multiple places ;)

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

14 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

Related Questions

Is it the same javascript 1 Answer

script default variable value behaviour 2 Answers

[Syntax] LINQ Query Operations in UnityScript ( Js ) 1 Answer

How to do transform.LookAt() from a different part of object? 1 Answer

Array of random loot items inside GUI.window 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