I am currently working on a equipment selection screen where the equipment sprites would appear when the user click on it. I have implemented the addEquipment function and added the code to the constructor class ( from the inventory script) and it works perfectly. Here is the code:
public class Inventory
{
private List<Equipments> equipmentlist;
public Inventory()
{
equipmentlist = new List<Equipments>();
AddEquipment(new Equipments { equipmenttype = Equipments.Equipmenttype.goggles, amount = 1 });
AddEquipment(new Equipments { equipmenttype = Equipments.Equipmenttype.powerpack, amount = 1 });
AddEquipment(new Equipments { equipmenttype = Equipments.Equipmenttype.goggles, amount = 1 });
//these 3 lines works fine
}
public void AddEquipment(Equipments equipment)
{
equipmentlist.Add(equipment);
}
public List<Equipments> GetequipmentList()
{
return equipmentlist;
}
}
I would like the button to call the function add equipment whenever the button is clicked. In the inspector of the button, it grabs the function from the player script, which has a code like this:
public class player : MonoBehaviour
{
[SerializeField] private UI_inventory uiInventory;
private Inventory inventory;
public void Awake()
{
inventory = new Inventory();
uiInventory.SetInventory(inventory);
}
public void AddGoggles()
{
inventory.AddEquipment(new Equipments { equipmenttype = Equipments.Equipmenttype.goggles, amount = 1 });
}
}
I can attach the AddGoggles function onto the Onclick() in the inspector, but the button doesn’t seem to work. Would appreciate if anyone could give some advice. Thanks.