Question by
PiterQ70 · Feb 22, 2017 at 05:55 PM ·
scripting problem
Remove item from List dont work
Hello I have problem with delete item from list. Adding work but Remove no:
public List<Lista> equippedItemsIDListLocal = new List<Lista>();
[Serializable]
public class Lista
{
public int playerID;
public int itemIDD;
public Lista(int newPlayerID ,int newItemID)
{
playerID = newPlayerID;
itemIDD = newItemID;
}
}
And I have Add and Remove with this lines:
equippedItemsIDListLocal.Add(new Lista(iID, itemID));
equippedItemsIDListLocal.Remove(new Lista(iID, itemID));
Comment
Best Answer
Answer by gameplay4all · Feb 22, 2017 at 07:24 PM
Since Lista
is a class, it is stored by reference in lists. But when you use the new
keyword you create a new instance of that class, so it has its own place in memory and thus has a unique reference. What you want to do is to iterate/loop trough the list and look for the item which has the correct playerID & itemIDD. Once you found the item use RemoveAt
to remove the item (you need its index).
Hope this helps!
-Gameplay4all
And Remove
removes by checking if the references are the same. And therefore doesn't find your 'new' Lista instance.