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 Dethras · Dec 25, 2013 at 10:12 PM · c#inputexceptioninputmanagerkeycode

Dynamic KeyCode Unity Exception

Hi, I am trying to make a simple input manager using C# and I got stuck in the KeyCode thing, here is the code

if (Input.GetKeyDown ((KeyCode)Enum.Parse(typeof(KeyCode), _controls[2]) )) {}

The _controls is a list of type string and at this moment in the [2] position I have "W".

And every time I start the game it gives me this exception "UnityException: Input Key named: W is unknown"

Can you please Help me with this problem Thanks

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
0

Answer by vexe · Dec 27, 2013 at 03:49 PM

I ran into this problem in a game jam we did - it was about a typing game. I ran into the very same exception. One fundamental thing I didn't realize at first, is that checking for if (Input.GetKeyDown("w)) will also return true if "W" was pressed (capital W) - In this case, if you want to detect if it was a capital or small, you'd have to check for the shift key as well with if (Input.GetKey(KeyCode.LeftShift))

Here's how I approached detecting the letters, I made an array of all the characters, and I looped over them and see which letter was pressed, I then check for the shift, as mentioned earlier:

 private string[] keys =
     {
         "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
         "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
         ".", ",", "/", ">", "<"
     };

 void Update()
 {
     for (int i = 0; i < keys.Length; i++) {
         var key = keys[i];
         if (Input.GetKeyDown(key)) {
             print(key + " was pressed");
             if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
                   print("and it's a capital " + key);
         }
 }

Not the prettiest, I know.

Then my partner @Jamora notified me of Input.inputString (doc) which I haven't got the time to test out - but it should act like, getch in C++ - It just returns the string that the user typed.

Hope that helps.

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 Dethras · Dec 27, 2013 at 04:12 PM 0
Share

Hi, I have just found the error. Thanks anyway :D

avatar image Rbert · Sep 25, 2014 at 05:55 PM 0
Share

sir can you help me..my game is like 4 pis 1 word..i have random value..how can i set the getkey in every letter..? is this one is applicable?

avatar image vexe · Sep 25, 2014 at 09:45 PM 0
Share

@Rbert could you explain more?

avatar image
0

Answer by T27M · Dec 25, 2013 at 10:16 PM

Are you trying to let the user change the key to use?

Unity has a built in input manager that can already does that.

Edit -> Project Settings -> Input

 if (Input.GetKeyDown("MyCustomInputName"))


Comment
Add comment · Show 9 · 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 Dethras · Dec 25, 2013 at 11:22 PM 0
Share

yes that's exactly what I am trying to do, but how can I make it possible to change it, do i have to enter each combination in the Input tab? or is there a way to access that data and change it via a script? also thanks for the fast response

avatar image T27M · Dec 26, 2013 at 12:23 AM 0
Share

When you build and run the game unity launches the input window seen in the link above. The user can customize the input settings from there. You don't have to change any code as you are using the name to refer to the button/key pressed.

If you want to do it during play time you would have to code something up which saves the new key layout. Take a look at this answer which uses the Event class to detect key presses.

There's also this, but I haven't used it so I can't comment on how good it is.

avatar image Dethras · Dec 27, 2013 at 11:54 AM 0
Share

The thing is that I am saving the controls of the user in a database (by using Event.Get$$anonymous$$ey) now I need to extract the data which is a string and convert it to keycode so that unity would recognize it.

avatar image T27M · Dec 27, 2013 at 12:28 PM 0
Share

Ok, I understand. Are you positive that "W" is at the correct array number? I tested your code with a local list and it's working fine for me.

avatar image Dethras · Dec 27, 2013 at 03:35 PM 0
Share

Yes I am positive, I have used the Debug.Log to check what it has as data and W is the only thing that comes up. Can you please show me the exact code you have used so I can check any mistakes I may have done. Thanks a lot

Show more comments

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

20 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

Related Questions

KeyCode parse problem 0 Answers

Key binding screen 0 Answers

Replacing the Input Manager Asset with your own 2 Answers

Unity built-in axes - how do they work? 1 Answer

Multiple Cars not working 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