Search Unity

removing item from list?

Discussion in 'Scripting' started by Phobiegames, Sep 14, 2019.

  1. Phobiegames

    Phobiegames

    Joined:
    Apr 3, 2017
    Posts:
    113
    Edit: Figured it out, Nevermind!

    Code (CSharp):
    1.     private void Start()
    2.     {
    3.         questItemList = new List<string>();
    4.         inventoryList = new List<ItemData>();
    5.     }
    6.  
    7.     public void AddItem(ItemData itemData)
    8.     {
    9.         inventoryList.Add(itemData);
    10.         Debug.Log(itemData.itemName);
    11.         return;
    12.     }
    13.  
    14.     public void RemoveItem(ItemData itemData)
    15.     {
    16.         inventoryList.Remove(itemData);
    17.         Debug.Log(itemData.itemName);
    18.     }
    So i have a list of itemData with a couple of functions so i can add or remove items, which i use this script on a ui button to do so:

    Code (CSharp):
    1. public class GiveItem : MonoBehaviour
    2. {
    3.     public string itemNameToTake;
    4.     public ItemData itemToGive;
    5.  
    6.     public void GivePlayerItem()
    7.     {
    8.         PlayerDataManager.instance.AddItem(itemToGive);
    9.     }
    10.  
    11.     public void TakePlayerItem()
    12.     {
    13.             PlayerDataManager.instance.RemoveItem(itemToGive);
    14.     }
    15.  
    16. }
    adding the items is working but removing the items isn't. Any idea why?
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    It's always a good idea to post the solution / mistake you found.
    That way others with a similar problem, who happen to find this thread in the future, can see if it's their problem as well.
     
  3. Phobiegames

    Phobiegames

    Joined:
    Apr 3, 2017
    Posts:
    113
    the solution is to make sure to add the same exact script to both buttons event functions