- Home /
Inventory problem
Hello everyone, I have a problem with my inventory system. The code looks if the object is in certain distance and has the tag "Item". If the user clicks E I get the Index of the object by getting the variable index in the script Index which is a component of every Objec ande the object is added to the inventory. All functions very well, but if some objects are added in the inventory and then put back in the scene again, everytime the user wants to add an object in the inventory, all obects that ever were in the inventory are added too, even if they are not in the certain distance... Here is my code:
void OnTriggerStay(Collider other) {
if (other.gameObject.tag == "Item") {
if (Input.GetKey (KeyCode.E)) {
other.gameObject.SetActive (false);
indexScript = other.gameObject.GetComponent<Index> ();
inventoryScript.AddItem (indexScript.index);
tipp.text = "";
}
//Here is the function AddItem in the inventoryScript but I think the error is in the code above:
public void AddItem (int id) {
for (int i = 0; i < inventory.Count; i++) {
if (inventory[i].itemName == null) {
for(int j = 0; j < database.items.Count; j++){
if(database.items[j].itemID == id) {
inventory[i] = database.items[j];
}
}
break;
}
}
}
I hope someone has an solution. Sorry for my bad english!
Could you show us how you remove items from inventory when you place them back to the world ?
Don't know if this fixes anything butI think you should put another break here
if(database.items[j].itemID == id)
{
inventory[i] = database.items[j];
break;
}
So you stop looking for items to add from database once you find it.
thanks for your answer. I checked your solution but the problem is still there.
here is remove item from inventory function:
void RemoveItem (int id) {
for (int i = 0; i < inventory.Count; i++) {
if (inventory[i].itemID == id) {
inventory[i] = new Item();
break;
}
}
}
it is called in another function: ...
if (e.is$$anonymous$$ouse && e.type == EventType.mouseDown && e.button == 1){
PlaceItem(slots[i].itemID,i);
RemoveItem(slots[i].itemID);
}
I have another if statement after the if (Input.Get$$anonymous$$ey (GetCode.E)) (see the code in my question) that checks if the Object that stays in the trigger and has the tag Item is active. If so there is a GUI Text enabled that says to click E to add the object. After added and removed an object again this text is always shown even if the critiacal object is in far distance...
Your answer
Follow this Question
Related Questions
Issues with Inventory script 0 Answers
Inventory armor wielding proplem,How to convert from derived to base 1 Answer
Inventory loop bolt 0 Answers
Inventory List Crafting HELP 0 Answers
Inventory question 1 Answer