Hi, I need some help. I checked the list and it is deleting the item in the list. It also sets all values to
null as wanted but when I set up the inventory with the list it gives back that
the “deleted item” is back in the list despite me not even seeing it in the inspector of the list.
Is Unity going too quickly through it?
Adding Items works but not deleting them.
Thank you for any help
The idea is to reset the inventory and load the list again with all non deleted items.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class inventory_player : MonoBehaviour
{
private void Awake()
{
DontDestroyOnLoad(this);
}
public List<pick_up_interactable> inventory_list;
public GeneralManager manager;
public AudioSource inventorySound;
public GameObject InventorySlots;
GameObject[] slots;
public GameObject Descriptions;
GameObject[] descs;
Sprite no_sprite;
public Color none_color;
private void Start()
{
inventory_list = new List<pick_up_interactable>();
no_sprite = null;
slots = new GameObject[5];
descs = new GameObject[5];
none_color = new Color(0, 0, 0, 0);
slots[0] = InventorySlots.transform.GetChild(0).gameObject.transform.GetChild(0).gameObject;
slots[1] = InventorySlots.transform.GetChild(1).gameObject.transform.GetChild(0).gameObject;
slots[2] = InventorySlots.transform.GetChild(2).gameObject.transform.GetChild(0).gameObject;
slots[3] = InventorySlots.transform.GetChild(3).gameObject.transform.GetChild(0).gameObject;
slots[4] = InventorySlots.transform.GetChild(4).gameObject.transform.GetChild(0).gameObject;
descs[0] = Descriptions.transform.GetChild(0).gameObject;
descs[1] = Descriptions.transform.GetChild(1).gameObject;
descs[2] = Descriptions.transform.GetChild(2).gameObject;
descs[3] = Descriptions.transform.GetChild(3).gameObject;
descs[4] = Descriptions.transform.GetChild(4).gameObject;
}
//Functions: AddItem(), RemoveItem()
public void WipeInventory()
{
for (int i = 0; i < inventory_list.Count; i++)
{
Debug.Log(i);
slots[i].GetComponent<Image>().sprite = no_sprite;
slots[i].GetComponent<Image>().color = none_color;
descs[i].transform.GetChild(0).gameObject.GetComponent<Text>().text = "";
descs[i].GetComponent<inventory_item_holder>().pick_Up = null;
}
SetUpInventory();
}
public void SetUpInventory()
{
for(int i = 0; i < inventory_list.Count; i++)
{
Debug.Log(inventory_list[i]);
slots[i].GetComponent<Image>().sprite = inventory_list[i].inventory_sprite;
slots[i].GetComponent<Image>().color = Color.white;
descs[i].transform.GetChild(0).gameObject.GetComponent<Text>().text = inventory_list[i].name_of_object + "\n" + inventory_list[i].description_of_object;
descs[i].GetComponent<inventory_item_holder>().pick_Up = inventory_list[i];
}
}
public void AddItem(pick_up_interactable item)
{
if (inventorySound != null)
{
inventorySound.Play();
}
inventory_list.Add(item);
WipeInventory();
}
public void RemoveItem(pick_up_interactable myitem)
{
for (int i = 0; i < inventory_list.Count; i++)
{
if (inventory_list[i] == myitem)
{
inventory_list.RemoveAt(i);
}
}
WipeInventory();
}
}