I want to make Items purchased from my game shop like decoration to go to Inventory after I purchase them.

Shop system

I have created a shop system with purchase buttons and stuff but I don't know how to make inventory receive the items these are the scripts I have for the shop and the inventory:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ShopManager : MonoBehaviour

    public int[,] shopItems = new int[9, 9];
    public float coins;
    public Text CoinsTXT;

    void Start()
        CoinsTXT.text = "" + coins.ToString();

        shopItems[1, 1] = 1;
        shopItems[1, 2] = 2;
        shopItems[1, 3] = 3;
        shopItems[1, 4] = 4;
        shopItems[1, 5] = 5;
        shopItems[1, 6] = 6;
        shopItems[1, 7] = 7;
        shopItems[1, 8] = 8;

        shopItems[2, 1] = 10;
        shopItems[2, 2] = 20;
        shopItems[2, 3] = 30;
        shopItems[2, 4] = 40;
        shopItems[2, 5] = 50;
        shopItems[2, 6] = 65;
        shopItems[2, 7] = 110;
        shopItems[2, 8] = 150;


    public void Buy()
        GameObject ButtonRef = GameObject.FindGameObjectWithTag("Event").GetComponent<EventSystem>().currentSelectedGameObject;

        if (coins >= shopItems[2, ButtonRef.GetComponent<ButtonInfo>().ItemID])
            coins -= shopItems[2, ButtonRef.GetComponent<ButtonInfo>().ItemID];
            CoinsTXT.text = "Coins:" + coins.ToString();



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Inventory {

    private List<Item> itemList;

    public Inventory()
        itemList = new List<Item>();


    public void AddItem(Item item)

What should I add to make it receive the purchased items?

  • \$\begingroup\$ Have you tried calling your AddItem from the shop script? Keep in mind your shop does not use item (whatever that is), but just ints \$\endgroup\$
    – Zibelas
    Commented Aug 14, 2022 at 16:03
  • \$\begingroup\$ i don't know how to do that do you have a script for that? or how to do it \$\endgroup\$ Commented Aug 14, 2022 at 16:17
  • 1
    \$\begingroup\$ We do not even know how your object item is declared. You would need to construct it from your int, get the reference from shop script to your inventory script (can be assigned via editor or found in start, same as you got the coinText) and call the AddItem from that reference once bought \$\endgroup\$
    – Zibelas
    Commented Aug 14, 2022 at 16:52

1 Answer 1


There are a few ways you could get this functionality. I would use the following method. You could have an event for when an item if purchased, then have the inventory subscribe to the event. You haven't shown any items or any way for the inventory to access any instance of the Item class, but I'll assume you have that mostly figured out since both your classes seem to use the Item class without issue.

For example, you could add the following event to your ShopManager class.

public static delegate PuchaseItemDelegate(Item item);
public static event OnPurchase;

Now that you have that event, you can have the inventory system subscribe to it, and have the inventory system call the AddItem method whenever an item is purchased. In your Inventory constructor, add the following line.

ShopManager.OnPurchase += AddItem;

Since OnPurchase is static, you don't need to have a reference to an instance of the class.

Now, all you have to do is invoke the OnPuchase event in your ShopManager class whenever an item is puchased, and it will call the AddItem method in the Inventory class. To do that, you use the same syntax you would use for a normal method. So within the if statement of the Buy method in your ShopManager class, add the following (where itemPurchased is the item related to the selected item id).


Alternatively, you could change the method signatures to have one int type parameter instead of an Item, then change the AddItem method in Inventory figure out what to do with the Id. I can't give you the most solid response on that since you haven't provided the code for the Item class.

To learn more about Events, you could read up on the documentation here

Sebastian Lague also has a great couple of videos on events and delegates

Video 1

Video 2

  • 1
    \$\begingroup\$ I would say event and delegates are a bit overkill/ above the skill of the poster. The question is essentially asking: how to call a function from a second script and events are a few levels above that. Never hurts to know about them but might not be the most helpful answer in this case. \$\endgroup\$
    – Zibelas
    Commented Aug 14, 2022 at 17:21
  • \$\begingroup\$ Fair enough, I'll leave it for people looking in the future. :) \$\endgroup\$ Commented Aug 14, 2022 at 19:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .