- Home /
Using items from inventory
Hello all my game is coming along great thanks to all the help from everyone in the community i am here again to ask for some guidance I downloaded the free inventory system that Brackeys made available for free https://www.assetstore.unity3d.com/#/content/10384 I got it into my game its working the only thing is that when I use say the water to replenish the thirst bar the hunger bar and energy bar also go up, and I only want to affect the thirst bar when I use the water here is the script
#pragma strict
//This script allows you to insert code when the Item is used (clicked on in the inventory).
var deleteOnUse = true;
private var playersInv : Inventory;
private var item : Item;
@script AddComponentMenu ("Inventory/Items/Item Effect")
@script RequireComponent(Item)
//This is where we find the components we need
function Awake ()
{
playersInv = FindObjectOfType(Inventory); //finding the players inv.
if (playersInv == null)
{
Debug.LogWarning("No 'Inventory' found in game. The Item " + transform.name + " has been disabled for pickup (canGet = false).");
}
item = GetComponent(Item);
}
//This is called when the object should be used.
function UseEffect ()
{
Thirst.tcurHP += 25;
Hunger.curHP += 25;
//Play a sound
playersInv.gameObject.SendMessage("PlayDropItemSound", SendMessageOptions.DontRequireReceiver);
//This will delete the item on use or remove 1 from the stack (if stackable).
if (deleteOnUse == true)
{
DeleteUsedItem();
}
}
//This takes care of deletion
function DeleteUsedItem()
{
if (item.stack == 1) //Remove item
{
playersInv.RemoveItem(this.gameObject.transform);
}
else //Remove from stack
{
item.stack -= 1;
}
Debug.Log(item.name + " has been deleted on use");
}
the problem starts at line 25 where I added the function to be able to use the pick ups any help on how to repair this will be greatly appreciated have a great weekend and happy game making. -K
In your game, what does this line do: Hunger.curHP += 25; Since Energy isn't mentioned at all in this script, is it perhaps tied to hunger in another script?
hello that is the line I put in to replenish the hunger, hunger is the script and curHP is the current hunger points, the 25 is the how much to add, the same with thirst.tcurHP+=25 its to replenish the thirst bar, but what is happening it does not matter witch one I use all the progress bars get filled, and I only want the water to affect the thirst bar and the food the hunger bar and so on...
Answer by getyour411 · Aug 25, 2013 at 01:12 AM
In UseEffect() you increase both without anything that would filter (i.e if/then, boolean etc) which to increase. I can only guess that Energy is tied to Hunger and eating gives you an energy boost since there's nothing else here that would affect Energy that I see.
@getyour411 I removed the energy.ecurHP+=25 and I think I understand about the boolean now I have to learn how to add them if you can point me to a reference or a tutorial to get me up to speed. $$anonymous$$uchas gracias
did you ever figure this out? im having the same issue and cannot get it to work....
Anyone get anywhere with this by now? I'm having the same problem, want multiple effects but only the ability for one effect
Your answer