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
0
Question by Entyro · Jan 11, 2015 at 10:23 PM · keypress

Need to press key twice to react

Hi!

This problem might be a tough thing to solve for you guys, and It's kinda hard to explain.

I have a script where I open a door with a password. The problem is when I added the password and then try to open the door it takes two presses on the "E" key to open. You can press the first "E" wherever you like, you don't have to be near the door (yes I have an "in range" in the script). After I pressed "E" one time then the seccond time I can open the door (if I'm near it).

As I said, this is really hard for you guys to understand, but I hope some of you guys know what I'm talking about.

Thanks in advance!

The script:

 var InRange;
 var buttonActivated;
 var cabinet : GameObject;
 var handle : GameObject;
 var cabinetIsOpen = false;
 var doorTimer : float = 0.0;
 public var guiSkin : GUISkin;
 
 //Sound
 var OpenSound : AudioClip;
 var CloseSound : AudioClip;
 var buttonSound : AudioClip;
 var unlockSound : AudioClip;
 private var hasPlayed = false;
 
 //Enabled/Disable
 var EnableDisable : EnableDisableComponents;
 
 //Password
 var passwordWritten : String;
 var newPassword : String;
 private var setNewPassword : boolean = false;
 private var tooShortPassword : boolean = false;
 private var newPasswordSet : boolean = false;
 private var wrongPassword : boolean = false;
 var thePassword: String;
 private var enterCabinet : boolean = false;
 private var enterPassword : boolean = false;
 private var correctPassword : boolean = false;
 var zero = "0"[0];
 var nine = "9"[0];
 
 
 function OnTriggerEnter (c : Collider)
 {
     InRange = true;
 }
 
 function OnTriggerStay (c : Collider)
 {
     InRange = true;
 }
 
 function OnTriggerExit (c : Collider)
 {
     InRange = false;
 }
 
 function OnGUI()
 {
     if(InRange == true)
     {
         GUI.skin = guiSkin;
         GUI.skin.settings.selectionColor = new Color(0.9, 0.7, 0.04);
         
         if(!enterPassword && !correctPassword)
         {
             GUI.Label (Rect (Screen.width/2-50, Screen.height - 100, 120, 50),"[E]  ENTER PASSWORD");
         }
         if(!cabinetIsOpen && correctPassword)
         {
             GUI.Label (Rect (Screen.width/2-50, Screen.height - 100, 120, 50),"[E]  OPEN CABINET");
         }
         if(cabinetIsOpen && passwordWritten == thePassword)
         {
             GUI.Label (Rect (Screen.width/2-50, Screen.height - 100, 120, 50),"[E]  CLOSE CABINET");
         }
         
 //Password lenght
         if(enterPassword)
         {
             var e = Event.current;
             if (e.isKey) 
             {
                 if (e.character < zero || e.character > nine)
                 {
                     e.character = 0;
                 }
             }
 //Set new password button
             if(GUI.Button (Rect (Screen.width - 300, 90, 270, 50), "SET NEW PASSWORD"))
             {
                 if(!setNewPassword)
                 {
                     setNewPassword = true;
                     newPassword = "";
                 }
                 else
                 {
                     setNewPassword = false;
                 }
             }
 //Set new password
             if(setNewPassword)
             {
                 wrongPassword = false;
                 GUI.Label (Rect (Screen.width/2 - 200, Screen.height/2 - 0, 500, 100), "SET NEW PASSWORD: ", "Password");
                 newPassword = GUI.TextField(new Rect(Screen.width/2 - 50, Screen.height/2 + 70, 200, 70), newPassword, 4);
                 if(GUI.Button (Rect (Screen.width/2 + 170, Screen.height/2 + 80, 100, 50), "SET"))
                 {
                     if(newPassword.Length == 4)
                     {
                         thePassword = newPassword;
                         setNewPassword = false;
                         audio.PlayOneShot(unlockSound);
                         newPasswordSetFunction();
                     }
                     else
                     {
                         tooShortPasswordFunction();
                     }
                 }
 //Text popup
                 if(tooShortPassword)
                 {
                     GUI.Label (Rect (Screen.width/2 - 200, Screen.height/2 + 130, 500, 100), "TOO SHORT!", "Password");
                 }
             }
 //Text popup
             if(newPasswordSet)
             {
                 GUI.Label(new Rect (15, 100 -50, 250, 50), "NEW PASSWORD:" + newPassword, "textPopup");
             }
             
 //Enter password
             GUI.Label (Rect (Screen.width/2 - 200, Screen.height/2 - 130, 500, 100), "ENTER PASSWORD:", "Password");
             passwordWritten = GUI.TextField(new Rect(Screen.width/2 - 50, Screen.height/2 - 60, 200, 70), passwordWritten, 4);
             if(GUI.Button (Rect (Screen.width/2 + 170, Screen.height/2 - 50, 100, 50), "ENTER"))
             {
                 if(passwordWritten == thePassword)
                 {
                     enterCabinet = true;
                     correctPassword = true;
                     setNewPassword = false;
                 }
                 else
                 {
                     wrongPasswordSetFunction();
                     setNewPassword = false;
                 }
             }
 //Text popup
             if(wrongPassword)
             {
                 GUI.Label (Rect (Screen.width/2 - 200, Screen.height/2 - 0, 500, 100), "WRONG PASSWORD!", "Password");
             }
         }
             if(passwordWritten == thePassword && enterCabinet)
             {
                 passwordFunction();
             }
     }
 }
 
 function Update ()
 {
     if(doorTimer > 0)
         doorTimer -= Time.deltaTime;
     if(doorTimer < 0)
         doorTimer = 0;
 
     if(InRange == true)
     {
         if(Input.GetKeyDown ("e"))
         {
             if(!enterPassword)
             {
                 enterPassword = true;
             }
             if(correctPassword)
             {
                 ToggleDoor();
                 enterPassword = false;
             }
         }
 }
 
     if(enterPassword)
     {
         EnableDisable.interaction = true;
         EnableDisable.disableComponents = true;
         EnableDisable.enableComponents = false;
     }
     if(correctPassword && InRange)
     {
         EnableDisable.interaction = false;
         EnableDisable.disableComponents = false;
         EnableDisable.enableComponents = true;
     }
 }
 
 //Password correct
 function passwordFunction()
 {
     if(!hasPlayed)
     {
         audio.PlayOneShot(unlockSound);
         hasPlayed = true;
         yield WaitForSeconds(0.5);
         enterPassword = false;
         correctPassword = true;
     }
 }
 
 //Open cabinet
 function ToggleDoor()
 {
     if(doorTimer == 0 && correctPassword)
     {
         doorTimer = 1.0;
         if (cabinetIsOpen == false)
         {    
             audio.PlayOneShot(OpenSound);
             cabinet.animation.Play("Cabinet open");
             handle.animation.Play("Cabinet handle");
             yield WaitForSeconds(0.9);
             cabinetIsOpen = true;
         }
 
         else
         {
             audio.PlayOneShot(CloseSound);
             cabinet.animation.Play("Cabinet close");
             yield WaitForSeconds(0.7);
             cabinetIsOpen = false;
             correctPassword = false;
             enterCabinet = false;
             passwordWritten = "";
             hasPlayed = false;
         }
     }
 }
 
 //Popups
 function tooShortPasswordFunction ()
 {
     tooShortPassword = true;
     yield WaitForSeconds(1);
     tooShortPassword = false;
 }
 
 function newPasswordSetFunction ()
 {
     newPasswordSet = true;
     yield WaitForSeconds(4);
     newPasswordSet = false;
 }
 
 function wrongPasswordSetFunction ()
 {
     wrongPassword = true;
     yield WaitForSeconds(1);
     wrongPassword = false;
 }



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 sam2k13 · Jan 11, 2015 at 10:32 PM 0
Share

Could you post the script?

avatar image Entyro · Jan 11, 2015 at 10:38 PM 0
Share

There you go!

avatar image sam2k13 · Jan 11, 2015 at 10:50 PM 0
Share

Try adding a debug.log () in the if (correctpassword) statement in your update function. Is there a response on the first keypress?

avatar image Entyro · Jan 12, 2015 at 11:16 AM 0
Share

There is no response on the first keypress

1 Reply

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

Answer by pako · Jan 12, 2015 at 01:22 PM

The problem has to do with the execution order.

You check for button press inside Update, and immediately after you check if correctPassword = true in order to ToggleDoor. However, correctPassword gets set to true inside OnGUI.

OnGUI runs after Update, see:

http://docs.unity3d.com/Manual/ExecutionOrder.html

Then, for correctPassword to get checked, you have to press the button again, because the check occurs only if the button has been pressed.

I think the solution is to add ToggleDoor() just after verifying the password.

Comment
Add comment · Show 14 · 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 Entyro · Jan 12, 2015 at 01:42 PM 0
Share

What do you mean by "outside the button-press check"?

avatar image pako · Jan 12, 2015 at 01:58 PM 0
Share

I mean outside if(Input.Get$$anonymous$$eyDown ("e")) :

 Update(){
 
 //some code
 
  if(Input.Get$$anonymous$$eyDown ("e"))
 {
 if(!enterPassword)
 {
 enterPassword = true;
 }
 }
 
 if(correctPassword)
 {
 ToggleDoor();
 enterPassword = false;
 }
 
 //some code
 
 }
avatar image Entyro · Jan 12, 2015 at 02:11 PM 0
Share

But then the door opens automatically when the password is correct. I want to press "E" so the door opens

avatar image pako · Jan 12, 2015 at 02:31 PM 0
Share

enterPassword = true gets set to true only if the "E"is pressed.

The password is entered and checked for correctness inside OnGUI (lines 127, 130), only if enterPassword = true (line 70) which means that "E" has been pressed, or the password wouldn't be entered and checked for correctness.

What am I missing?

avatar image Entyro · Jan 12, 2015 at 02:44 PM 0
Share

If I have...

 if(correctPassword)
 {
 ToggleDoor();
 enterPassword = false;
 }

... outside the "E" button press, then the door opens automatically as I said. It's because when I press Enter on the button "correctPassword" sets to true. And the code above gets called because the "correctPassword = true"

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

27 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

Related Questions

Build is always on Free Aspect, cant change it 1 Answer

I open my project and I found that everything's gone?! 2 Answers

When i run Unity3D leaves a freak message 1 Answer

How to ground the player after jump animation completes 0 Answers

how can i make a ball bounce when i click on it 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