Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Chesley · Oct 02, 2011 at 10:18 AM · stringconvertkeycode

Another KeyCode problem...

How can i convert like a normal string "KeyCode.A" to a KeyCode?

static var left : KeyCode = KeyCode.A;

i am sending the string "KeyCode.A" to the variable left in .....left = "KeyCode." + optionsleftkey;

optionsleftkey = A

Comment
Add comment · Show 3
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 save · Oct 02, 2011 at 10:21 AM 0
Share

Is there a special reason why you're not using Input for this?

avatar image Chesley · Oct 02, 2011 at 10:23 AM 0
Share

yes i use Input in if(Input.$$anonymous$$eyCode(left)) get it? thats so the key for left is changeable ingame

avatar image save · Oct 02, 2011 at 10:24 AM 0
Share

Ah I see! Let me think a bit

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by save · Oct 02, 2011 at 10:41 AM

Here's an updated version, note that this is strictly made out of testing, so you'd have to make a function for setting the key and getting the key separate.

#pragma strict

static var leftKey : String;

function OnGUI () { var e : Event = Event.current; if (e.isKey && Input.anyKeyDown && e.keyCode.ToString()!="None") { SetKey (e.keyCode.ToString()); }

 if(e.keyCode.ToString() == leftKey) {
     transform.position.x+=10*Time.deltaTime;
 }

}

function SetKey (incomingKey : String) { leftKey = incomingKey; Debug.Log(leftKey.ToString()); }

Basically just compare the strings. I might be able to give you a better example later where you compare directly against the actual KeyCode without conversion.

To later work with KeyCodes you can parse the KeyCode (which is an enum):

System.Enum.Parse(typeof(KeyCode), theString)
Comment
Add comment · Show 5 · 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 Chesley · Oct 02, 2011 at 10:43 AM 0
Share

Thanks! i will try it out then i will set this question as awnsered! :D

avatar image save · Oct 02, 2011 at 10:57 AM 0
Share

Sorry I'm in a bit of a hurry (hence the mistake), hold on let me see if I can get it straight

avatar image save · Oct 02, 2011 at 11:24 AM 0
Share

Updated it with a quick testing script, do note that setting and getting is done in the same block and done from the GUI which is not preferable. I'll get back to you if you don't come up with a solution for your implementation.

avatar image BerggreenDK · Oct 29, 2011 at 06:43 PM 1
Share

this code: e.keyCode.ToString()!="None" is a rather ugly way of deter$$anonymous$$g if there is a keycode. Why not use $$anonymous$$eyCode.None ? http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$eyCode.None.html

avatar image Eric5h5 · Oct 29, 2011 at 08:50 PM 1
Share

Also mixing Input functions into OnGUI isn't really a good idea. Use events ins$$anonymous$$d.

avatar image
0

Answer by SupaFinga · Apr 20, 2013 at 02:45 AM

why don't you just declare your leftKey as a KeyCode instead of a String? Just tested it, works perfectly (in 4,0, IDK about previous versions)

Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Converting string of a key into KeyCode 2 Answers

Another Error - Enum.Parse 1 Answer

convert string to int 0 Answers

Disable a Key after shooting 3 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