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 FusionSausage · Oct 20, 2013 at 11:15 AM · c#keydetectkey pressedpress

How would I detect if key get pressed 2 times

Hi, I have a variable which grants the player access to place objects. Now, if I for example press B, it should grant access to the player to place objects, and if I press B again it should remove access, and so on so forth. What's the best way to do this?

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

1 Reply

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

Answer by vexe · Oct 20, 2013 at 11:21 AM

Just use a bool, and then toggle it.

 bool grantAccess = false;
 
 if (Input.GetKeyDown(KeyCode.B))
    grantAccess = !grantAccess;

 // somewhere else
 if (grantAccess)
    placeObjects();
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 FusionSausage · Oct 20, 2013 at 11:25 AM 1
Share

Oh, it was that simple! Thank you good sir :)

avatar image vexe · Oct 20, 2013 at 11:28 AM 0
Share

Anytime :) - Also, as meat5000 suggested, make sure you know the differences of each of Input.GetXXX family members, see this.

avatar image meat5000 ♦ · Oct 20, 2013 at 11:30 AM 0
Share

Yeah I wasn't gonna spell it out. By the time I submitted, your answer had already been accepted :D So i deleted it :P

Interestingly, it still says 2 answers. One for the bug thread.

avatar image vexe · Oct 20, 2013 at 12:52 PM 1
Share

Well, just initialize it to true ins$$anonymous$$d:

 bool grantAccess = true;

And you don't need the else-if, just an else, if the if condition was false, execution would automatically jump to the else part:

 if (grantAccess) {
   CursorGrid2.meshActive = 1;
 }
 else {
   CursorGrid2.meshActive = 0;
 }

Better yet, use the ternary operator:

  CursorGrid2.meshActive = grantAccess ? 1 : 0;

This mean: is grantAccess true? if so, set CursorGrid2.meshActive to 1, else 0.

avatar image FusionSausage · Oct 20, 2013 at 01:18 PM 1
Share

Yes. I should actually use booleans, but from my years of program$$anonymous$$g I've barely touched booleans, I've always used integers. So I will try to use booleans more, thanks! Now, what you helped me with was that I need to allow the user to place objects at certain points, for example when you press the build button (b). When you press it, it should allow you to build and when you press it again you should not be able to build. I solved this in the beginning by having if statements at every part of the code which controlled the placement functions, and to call it I had one button to allow and one to decline. I didn't see that as a good way to have it, because almost every game nowadays have that function, so I thought that it shouldn't be that hard to do. So you've helped me with that and I'm truly graceful, thank you!

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

17 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

Related Questions

Distribute terrain in zones 3 Answers

Seven key press 3 Answers

Multiple Cars not working 1 Answer

How do I get my door to work? 1 Answer

How do I play an animation with the 'E' key when I enter a trigger 2 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