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
1
Question by Bluestrike · Jan 21, 2013 at 02:35 PM · stringenum

String to enum

Can I make my string into a enum in javascript>

it would make this switch function unnessesary and I won;t have to edit it when i possibly add an element to an enum.

 @RPC
 function SetStation(setToo :String) :void
 {
     switch(setToo)
     {
         case("Jump"):
             currentUpgrade = Upgrades.Jump;
             break;
     }
 }    

Upgrades is my enum and I wonder if I could have something like Upgrades. + the jump string = Upgrades.Jump

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

Answer by Paulius-Liekis · Jan 21, 2013 at 03:08 PM

Google says this:

 var value : Upgrades = (Upgrades) Enum.Parse(typeof(Upgrades), setToo );  
Comment
Add comment · Show 4 · 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 Bluestrike · Jan 21, 2013 at 04:02 PM 0
Share

The (upgrades) gave me a compiling error but this seems to work (Declared currentUpgrade earlyer)

currentUpgrade = Enum.Parse(typeof(Upgrades), setToo );

avatar image Bunny83 · Jan 21, 2013 at 04:29 PM 0
Share

Yes, UnityScript doesn't have the "c-style" cast. UnityScript will cast it implicitly.

avatar image Metal_Homer · Nov 15, 2016 at 03:05 PM 0
Share

Does this look different in C# because it tells me it doesn't´t now Enum. It doesn't´t work with enum as well. I´m using System.Collections.

avatar image Landern Metal_Homer · Nov 15, 2016 at 03:06 PM 0
Share

the Enum class doesn't exist in System.Collections, it exists in System(https://msdn.microsoft.com/en-us/library/system.enum.parse(v=vs.110).aspx), put a using statement to include System

 using System;

or qualify the name space

 System.Enum.Parse(etc.....
avatar image
0

Answer by Owen-Reynolds · Jan 21, 2013 at 04:36 PM

The traditional method is to treat enums as ints, when needed. Say the enum is {"walk", "run", "jump"}. That's just a fancy way so we don't have to write if(up==0) and have to remember that upgrade #0 is walk. But when you write up=Upgrade.walk to computer really stores it as up=0.

So you'd send the RPC: int upNum = (int)setToo, which would send a 2 for "jump". Then translate back using Upgrade up=(Upgrades)upNum.

The funny thing is, the number will have been a 2 the entire time. All the castes are doing is telling the computer that it's OK to use the "real" 0,1,2 code numbers.

But an RPC is already so slow and uses a minimum 4K(?) anyway, that sending "the long way" as a string isn't going to matter unless you send hundreds each frame.

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 Bluestrike · Jan 21, 2013 at 05:25 PM 0
Share

The string is com$$anonymous$$g from a string Array (not every upgrade will be availeble in every level) and I also need the string value again for something else later in the function too. I am using the int values to send enum values in rpc calls and your right I supose it could be a int in this case as well :)

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

String.Split problem 1 Answer

C# Incrementing a String Array 1 Answer

String Assets? 1 Answer

How to use Enum? 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