Calling a function from a button click

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.

define “doesnt work”

We know buttons work and you have a total of about six maybe eight SLOC (significant lines of code) above.

Break it down, build it up, find what’s going wrong. Maybe you put a Text field in front of the button that has Raycast Target enabled? Maybe you dragged the prefab of something in and you’re happily calling the prefab instead of the real one?

Whatever it is, it just Sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.