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 /
  • Help Room /
avatar image
0
Question by Psy_boy2720 · May 31, 2016 at 09:35 AM · keycode

Unity does not register first key press

I'm VERY new to the Unity community and programming and I'm following a course which is awesome, but I've run into some issues when making my first text adventure game.

I'm using the following code to get back and it works fine.

if (Input.GetKeyDown(KeyCode.T))
{ myState = States.hule;}

Where "hule" is the Danish word for Cave.

I use the EXACT same syntax for when I want to "see the sheets"

if (Input.GetKeyDown(KeyCode.L))
{myState = States.lagen_0;}

where "lagen" is sheets in Danish. When I press L to view sheets, i Have to press it twice, every time, or else nothing happens.

I'm using Visual Studio as the programming "studio".

I hope someone can help with this issue.

Comment
Add comment · Show 1
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 Guhanesh · May 31, 2016 at 09:44 AM 0
Share

post your full code.this code looks ok.

1 Reply

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

Answer by Psy_boy2720 · May 31, 2016 at 09:49 AM

Even though its over 130 lines of code? I must mention that I haven't written any comments on the code either.

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 Psy_boy2720 · May 31, 2016 at 09:55 AM 0
Share

I've seen that others have done the same, so here goes.

 public Text text;
     private enum States {hule, spejl, dør_0, lagen_0, hule_spejl, dør_1, lagen_1, frihed};
     private States myState;
     // Use this for initialization
     void Start(){
         myState = States.hule;
    }
 
     // Update is called once per frame
     void Update()
     {
         print(myState);
         if (myState == States.hule)                 {state_cell();}
         else if (myState == States.lagen_0)         {state_lagen_0();}
         else if (myState == States.lagen_1)         {state_lagen_1();}
         else if (myState == States.spejl)           {state_spejl();}
         else if (myState == States.hule_spejl)      {state_hule_spejl();}
         else if (myState == States.dør_0)           {state_dør_0();}
         else if (myState == States.dør_1)           {state_dør_1();}
         else if (myState == States.frihed)          {state_frihed(); }
 
     }
 
     void state_cell ()
     {
         
             text.text = "Du er fanget i et fængsel og du vil gerne flygte. " +
                         "Der ligger et snavset lagen på hvad der ligner " +
                         "en seng, et spejl hænger på væggen og en dør der er låst " +
                         "udefra.\n\n" +
                         "Tryk L for at Se Lagenet, S for at se Spejlet og D for at se Døren ";
                
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.L))        {myState = States.lagen_0;}
         else if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S))   {myState = States.spejl;}
         else if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D))   {myState = States.dør_0;}
     }
 
     void state_lagen_0() 
     {
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.L))
         {
             myState = States.lagen_0;
             text.text = "Du fatter ikke at nogen kan sove her, nogen må da skifte lagenerne. " +
                         "Det er vel bare fængsels livets 'glæder' ! \n \n " +
                         "Tryk T for at komme Tilbage";
 
         }
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.T))        {myState = States.hule;}
 
     }
     void state_spejl()
     {
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S))
         {
             myState = States.spejl;
             text.text = "Du ser et snavset spejl. Det sidder lidt løst. \n \n" +
                         "Tryk G for at tage spejlet. " +
                         "Tryk T for at komme Tilbage";
 
         }
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.T))        {myState = States.hule;}
         else if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.G)) {myState = States.hule_spejl;}
 
     }
 
 
avatar image Guhanesh Psy_boy2720 · May 31, 2016 at 10:55 AM 0
Share

All Inputs should be checked only in Update function. yes according to your code , when first time L is pressed state_lagen_0() but Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.L) is false inside state_lagen_0() because Input.Get$$anonymous$$eyDown is true for only one frame or one update(). change Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.L) to Input.Get$$anonymous$$ey($$anonymous$$eyCode.L) it will work because Input.Get$$anonymous$$ey will be true for all the frames the key is held down.

 void state_lagen_0() 
         {
                 if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.L))
                 {
                         myState = States.lagen_0;
                         text.text = "Du fatter ikke at nogen kan sove her, nogen må da skifte lagenerne. " +
                                 "Det er vel bare fængsels livets 'glæder' ! \n \n " +
                                 "Tryk T for at komme Tilbage";
 
                 }
                 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.T))        {myState = States.hule;}
 
         }


but keep in $$anonymous$$d inputs should always be checked in update function only.

avatar image Psy_boy2720 · Jun 01, 2016 at 05:33 AM 0
Share

Omg it worked! :)

So, just to be sure that I know what you mean.

The first time a new key press is "mentioned" it should be input.Get$$anonymous$$eydown($$anonymous$$eycode.[]) correct? and after the first statement, it should be input.Getkey(keycode.[]) ?

Am I understanding this correctly?

avatar image Guhanesh Psy_boy2720 · Jun 01, 2016 at 05:56 AM 0
Share

yes. It is a temporary fix.

Hereafter when you code call input.Get$$anonymous$$eydown or input.Getkey only in Update ()

 void Update()
 {
 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.L))
 {
 //do something
 }
 if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.L))
 {
 //do something
 }
 
 }

but calling in other functions is not good practice.

  void state_lagen_0() 
          {
  if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.L))
     {
     //do something
     }
 
 }

above code is not recommended

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

58 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 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 avatar image avatar image

Related Questions

"event.current.keyCode" not work! 0 Answers

How to change Button input to Keyboard input? 1 Answer

Joystick buttons don't work 0 Answers

Droping a box by pressing space or any key 1 Answer

Can't return KeyCode in build 0 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