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 Sudhi · Jul 24, 2012 at 08:41 PM · disablekeykeycode

Disable a Key after shooting

I got the code to disable a key when the event is happening from this link, http://forum.unity3d.com/threads/25033-how-to-disable-a-keyboard-key

What I want is, disable the up arrow key after using it once, and then when the object instantiated comes back to the spawnlocation, to activate it again to throw the object.. how do i do this??

Please Helppp!! I have tried triggers to check trigger enter and trigger exit doesnt help deactivating.. the object still spawns.. I Need Helppppp!!!

Thanks in advance..

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 GC1983 · Jul 24, 2012 at 08:42 PM 0
Share

Tried using a boolean?

3 Replies

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

Answer by Bovine · Jul 24, 2012 at 08:55 PM

Something like:

 public bool IsKeyEnabled_W { get; set; }
 
 void Awake()
 {
    IsKeyEnabled_W = true;
 }
 
 void Update()
 {
    if(IsKeyEnabled_W)
    {
       if(Input.GetKeyUp(KeyCode.W))
       {
          // *** DO SOMETHING HERE!!!! ***
       }
    }
 }

And then when you wish to disable the key do:

 IsKeyEnabled_W = false;

And to enable it again:

 IsKeyEnabled_W = true;

Does that make sense?

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 Sudhi · Jul 24, 2012 at 08:58 PM 0
Share

Yep.. Got it.. it worksssss...!!! Thanks a lot.. ! :)

avatar image Bovine · Jul 24, 2012 at 09:01 PM 0
Share

no worries. You could get a bit cleverer with this if you wanted so that you didn't have to have a property per key - have a dictionary of key codes to booleans and you could use one pair of methods to enable and disable keys. I don't know how many key codes there are, but you could also used a fixed size array to do the same - a little more memory hungry but a bit more efficient.

avatar image Sudhi · Jul 24, 2012 at 09:06 PM 0
Share

True.. but currently I only have 4 keys in the game.. but will keep that in $$anonymous$$d for future purposes.

avatar image Bovine · Jul 24, 2012 at 09:06 PM 0
Share

Yep - for such a small number, it's hardly worth it :oD

avatar image
0

Answer by DaveA · Jul 24, 2012 at 08:47 PM

I wouldn't use that script, but the other suggestion there, which is, just don't respond to the key. So you have, in Update or OnGUI, some code you want to do something when UpArrow is hit. And you know some condition under which you don't want to respond. So don't. Eg

 if (itsOkToRespondToUpArrow && Input.GetKeyDown(KeyCode.UpArrow))
 {
    ... do the uparrow thing...
 } // else not

Either set such a boolean variable, or make whatever function calls or check whatever variables are involved like that.

Comment
Add comment · Show 1 · 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 Bovine · Jul 24, 2012 at 09:00 PM 0
Share

I agree, that looks clever but maybe a little too clever for my liking!

avatar image
0

Answer by Sudhi · Jul 24, 2012 at 09:07 PM

yeyeeee... as soon as u said boolean, I got the idea.. I add a new variable

 var useKeyBoard = true;
 if(Input.GetKeyUp(KeyCode.UpArrow)) {
 if(useKeyBoard == true) {
 //code logic
 useKeyboard = false;
 }
 }
 if(Input.GetKeyDown(KeyCode.DownArrow)) {
 //code logic
 useKeyboard = true;
 }

It WORKSSS.....!!!! Thank you thank you thank you... (^_^)

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 Bovine · Jul 24, 2012 at 09:09 PM 0
Share

As much as I welcome your enthusiasm, I'm not sure what adding another answer, essentially saying the same as the two existing answers does. It's also not terribly well formatted :o/

avatar image Sudhi · Jul 24, 2012 at 09:12 PM 0
Share

I acutally added this answer after I got the comment from GC1983... but it got approved now.. after all our discussions.. sorry for that.. !

avatar image Bovine · Jul 24, 2012 at 09:13 PM 0
Share

Fair enough - why not tidy the code at least? :o)

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

7 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

Can someone explain this?? KeyCode Error 1 Answer

Disable child with name of a specific object 1 Answer

Unity will not run when I assign an animation to a keystroke 0 Answers

Deactivate Key. 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