- Home /
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;
}
}
}
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.
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;
}
}
}
}
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.
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.
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.
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!