- Home /
Button press wipes out my inventory
Since a search, and fiddling didn't fix the problem, I figured I'd ask the folks who know what they're doing. (and I thank God that you do!) Using a gamepad, I go into my inventory. Let's say I want to eat a piece of toast, and I have four pieces in said inventory. I have the "eat toast" command assigned to button 2. If I press button 2, however, I'll instantly eat all four pieces of toast. Have tried changing input sensitivity, gravity, dead space, though I figured only sensitivity would be relevant. Nothing ever changed though, and I'm hoping someone might know what I'm doing wrong. If it helps, I'm using a Logitech Gamepad F310 controller. Thanks, and God bless.
EDIT: Code below. BTW: Yes, I realize it's a mashup. Am in the middle of changing it from array based to static var based to make it more (newb) friendly with PlayerPrefs.
static var inventoryArray : int[] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var inventoryText : GameObject;
static var HardTack : int = 1;
function Update () {
inventoryText.guiText.text = "Health Potion " + "[" + inventoryArray[0] + "]" + "\n" + "Hard Tack " + "[" + HardTack + "]" + "\n" + "Water " + "[" + inventoryArray[2] + "]" + "\n" + "Apple Brew " + "[" + inventoryArray[3] + "]" + "\n";
if(Input.GetButton("Stuff"))
if(inventoryArray[0] > 0) {
healthPotion();
}
if(Input.GetButton("Sword Slash"))//Right HERE
if(HardTack > 0) {
hardTack();
}
}
//inventoryArray[0]++;
//inventoryArray[1] ++;
function healthPotion () {
Playerhealth.curHealth += 15;
inventoryArray[0] -=1;
}
function hardTack () { //RIGHT HERE
Playerhunger.curHunger -= 5;
HardTack -=.5;
}
Answer by fafase · Oct 11, 2013 at 06:43 PM
Since you have no code shown on your question, I go with a guess:
you have
if(Input.GetKey(KeyCode.JoystickButton2)){ // eat only one of those toasts}
That should eat only one but since the method returns true each frame and a frame is way too fast for you to press only once, then it returns for as long as you pressed and your 4 toasts are gone in 4 frames, a few ms.
Try with :
if(Input.GetKeyDown(KeyCode.JoystickButton2)){ // eat only one of those toasts}
If it is not the case then you need to post your code(which you should have done in the first place).
thanks fafase. I figured that this was an settings - input issue, hence no code. Totally and honestly sorry, didn't want to not give folks the tools to help. Will edit to include code.
EDIT: fafase, you're a genius. That did the trick.
Your answer