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 Borzi · Mar 12, 2014 at 08:38 PM · inventoryfloatlistsweapongetkeydown

How to recognise the number key that was pressed

Hey! I am working on an inventory system. What I am trying to do is pretty hard to explain, and I think my code example does it best: I am trying to get the game to recognise when the player inputs a number (from 1 to 10) and save that number. It then should check if the current weapon is equal to that key which was pressed and if not, if the weapon that is assigned to that key was unlocked. Following to that, it should set "CurWeapon" equal to that key. It would really help me out A LOT if someone could show me how to do this...

         if(Input.GetKeyDown(*a number key from 1 - 10*) && transform.GetComponent<WeaponStates>().Reloading == false)
         {
             if(CurWeapon != *that key*)
             {
                 if(wp.WeaponKey == *that key* && wp.Unlocked == true)
                     {
                     CurWeapon = *that key*;
                     LastWeapon = WeaponPlaceHolder;
                     WeaponPlaceHolder = CurWeapon;
                     }
             }
         }
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
1
Best Answer

Answer by stevethorne · Mar 12, 2014 at 08:43 PM

You could do something like this

 for ( int i = 0; i < 10; ++i )
 {
     if ( Input.GetKeyDown( "" + i ) )
     {
         
     }
 }

Then if you store your weapons in an array, you can access them using "i" in the if statement and set everything up properly.

Comment
Add comment · Show 6 · 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 Borzi · Mar 12, 2014 at 08:47 PM 0
Share

Thanks for your reply! Could you briefly explain what this code does exactly? I have no chance to try it out to its full extent but I am just going to reckon that it will work as I am getting no errors. Update - The code does not seem to be working correctly :/ Here is what I added:

 for(int i = 0; i <= 10; ++i)
         {
         if(Input.Get$$anonymous$$eyDown("" + i) && transform.GetComponent<WeaponStates>().Reloading == false)
             {
             if(CurWeapon != i)
                 {
                 if(wp.Weapon$$anonymous$$ey == i && wp.Unlocked == true)
                     {
                     CurWeapon = i;
                     LastWeapon = WeaponPlaceHolder;
                     WeaponPlaceHolder = CurWeapon;
                     }
                 }
             }
         }
avatar image stevethorne · Mar 12, 2014 at 09:05 PM 0
Share

Without knowing what your CurWeapon and Weapon$$anonymous$$ey variables are I can't really help you. The code just goes through 0-9 and checks to see if those keys are down. "i" is going to represent the number of the key the loop is currently on. So you'll need to assign your weapons using the number of the key that was pressed. I recommend putting the weapons in an array so that you can choose them by using something like "weapons[i]" and set that to your current weapon.

avatar image Borzi · Mar 12, 2014 at 09:17 PM 0
Share

Well that's exactly what is going on in this script. CurWeapon is basically just to check what number was last pressed and switches to a different one if that item is not available. Weapon$$anonymous$$ey (I am using a class as I usually prefer lists over arrays) just assigns the weapon to a certain key. Therefore if Weapon$$anonymous$$ey == the key that was pressed, change the weapon (if that weapon was unlocked, of course). Is there a way that I can debug what key was pressed? Because that way I could tell if it was my code or yours that is causing the issue.

avatar image stevethorne · Mar 12, 2014 at 09:33 PM 0
Share

Of course, just add a Debug.Log ( i ) inside the getkeydown if statement. Or you could attach the debugger to unity and use breakpoints to step through and see what's happening.

avatar image Borzi · Mar 12, 2014 at 09:41 PM 1
Share

I fixed my problem, thank you VERY much! This will help me a lot in creating my game :) I wish you a good life sir!

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

22 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

Related Questions

I'm trying to make a button that instantiates an item when you craft it 1 Answer

How to stack items into your inventory? unsolved 1 Answer

My script for pick up items work is wrong! Help please 1 Answer

GUI Button Position - Can it float to the top-left? 3 Answers

Serialized class, Instances and access 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