Removing item from list makes it appear at the bottom ?
I am trying to create an inventory box not unlike the original resident evil games, I can put items in with no issues. My problems occur when i try to remove an item. the item will be removed from the box and added to my inventory but for some reason the item will spawn at the bottom of the list. (there is a secret extra slot that the guns hiding in).
if (inventory.ItemStorage [slotNumber].itemName == null )
{
item = null;
itemAmmo = 0;
ItemId = 0;
itemIcon.enabled = false;
itemIcon.sprite = null;
ammoSet = false;
itemCount.count = 0;
itemNameText.text = "";
}
if (inventory.ItemStorage[slotNumber].itemName != null)
{
item = inventory.ItemStorage[slotNumber];
itemIcon.enabled = true;
itemIcon.sprite = inventory.ItemStorage[slotNumber].itemIcon;
itemNameText.text = item.itemName;
itemCount.count = item.itemAmmo;
Debug.Log (slotAmmo + " " + slotNumber);
if (item.itemType == Items.ItemType.Key)
{
itemCount.itemTotal.text = "";
ammoSet = true;
}
if (ammoSet == false)
{
itemAmmo = myData.ammo;
ammoSet = true;
}
}
else
{
itemIcon.enabled = false;
if (itemCount.count == 0)
{
itemCount.itemTotal.text = "";
}
}
}
public void OnSubmit()
{
inventory.ItemStorage.Remove(item);
inventory.itemAmmo = itemCount.count;
inventory.removeFromStorage = true;
inventory.removeFromStorageAmmoSet = true;
inventory.addItem (item.itemID);
//inventory.removeItemFromStorage (item.itemID);
//inventory.slotNumber = slotNumber;
}
}
When the slot is clicked it will run the OnSubmit function. I have tried the .Remove, .RemoveAt and making the slot null all to no avail.
public void addItem(int id)
{
for (int i = 0; i < dataBase.items.Count; i++)
{
if (dataBase.items [i].itemID == id)
{
Items item = dataBase.items [i];
addToEmptySlot (item);
break;
}
}
}
void addToEmptySlot(Items item)
{
for (int i = 0; i < Item.Count; i++) {
if (Item [i].itemName == item.itemName && item.itemType == Items.ItemType.Ammo) {
stackAmmo (item);
stacked = true;
break;
}
}
for (int i = 0; i < Item.Count; i++)
{
if (Item [i].itemName == null && removeFromStorage == false && stacked == false)
{
Item [i] = item;
//Item [i].itemAmmo = itemAmmo;
Item.Add (item);
break;
}
else if (Item [i].itemName == null && removeFromStorage == true && stacked == false)
{
//ItemStorage.Remove (item);
Item [i] = item;
defaultInt = Item [i].itemAmmo;
Item [i].itemAmmo = itemAmmo;
Debug.Log (Item [i].itemAmmo);
Item.Add (item);
//ItemStorage.Remove (item);
//Item [i].itemAmmo = defaultInt;
removeFromStorage = false;
break;
}
}
stacked = false;
}
I have spent a day and as many fixes as I could conjure, sorry if the post doesn't adhere to standards, this is my first one.
So bluntly, when i click the storage slot the item should enter my inventory and be completely removed from storage.
If you need more info, simply ask and thanks in advance :).
Answer by 12ssmith1 · Jul 24, 2016 at 09:07 PM
It's okay, all i needed was a break. Turns out i was adding it twice. Anywho problem solved.
Your answer
Follow this Question
Related Questions
How Can Storage Be Done If Unity Deletes Or Duplicates Gameobjects? 0 Answers
How do I pull variables from an object in a list? 1 Answer
Save gameobjects in a list before destoying 0 Answers
Using foreach to remove and delete bullet in List - C# 3 Answers
All crops in list growing at once when they are supposed to grow individually 0 Answers