• 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
2
Question by Funlamb · Mar 01, 2014 at 05:30 PM · c#keycode

String to KeyCode in C#

Hey guys,

I've been working on t$$anonymous$$s for an hour now. I'm sure it's simple but my lack of knowledge seems to get the best of me.

I'm trying to get a string to get converted into a KeyCode. Here is my code:

 using UnityEngine;
 using System.Collections;
 using System;

     public string joystickButton = "JoystickButton9";
     public KeyCode t$$anonymous$$sKeyCode = Enum.Parse(KeyCode, joystickButton);

My big plan later will be to concatenate these strings so that I can add t$$anonymous$$s base script to a character. Then change the characters joystick and control $$anonymous$$m with that.

pseudo code:

 public int player = 4
 joystickButton = "Joystick" + player.toString() + "Button9";
Comment
Add comment · Show 4
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 Burk · Mar 01, 2014 at 05:32 PM 0
Share
avatar image Linus · Mar 01, 2014 at 05:35 PM 0
Share
avatar image Funlamb · Mar 01, 2014 at 06:29 PM 0
Share
avatar image logicandchaos · 6 days ago 0
Share

why use strings at all? you can use variables of type KeyCode.

2 Replies

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

Answer by Lo0NuhtiK · Mar 01, 2014 at 05:43 PM

KeyCode t$$anonymous$$sKeyCode = (KeyCode) System.Enum.Parse(typeof(KeyCode), "Whatever") ;

Comment
Add comment · Show 7 · 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 Funlamb · Mar 01, 2014 at 06:25 PM 0
Share
avatar image Lo0NuhtiK · Mar 01, 2014 at 08:52 PM 0
Share
avatar image Funlamb · Mar 02, 2014 at 03:47 AM 0
Share
avatar image Josh707 · Mar 02, 2014 at 07:01 AM 0
Share
avatar image Lo0NuhtiK · Mar 02, 2014 at 08:22 AM 0
Share
Show more comments
avatar image
0

Answer by k3nt741 · 6 days ago

 KeyCode code = (KeyCode) System.Enum.Parse(typeof(KeyCode), "Whatever", true) ;

It wasn't working for me until i added the other parameter, i was trying to save my keycodes in my Unity project from strings.

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 · 6 days ago 1
Share

When you post such a solution you should at least clarify what this "parameter" does. The optional boolean parameter "ignoreCase" would ignore the casing of the string. Usually you would not need that unless you somehoe changed the string or it was input manually with the wrong casing.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

28 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

I need help with condensing code 2 Answers

C# - Keycode sequence doesn't really work 0 Answers

problem with mapping keys through c# 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