Items purchased not Appearing in the inventory when they're purchased

Viewed 19

I purchase Items from my shop menu In-Game it doesn't show in my Inventory these are the pictures from the game shop and when i purchase something and when i open the inventory menu:

Opening Shop menu to buy an Item

When i purchase the item and the money is subtracted from my Coins text

Empty Inventory Menu after purchase

These are the scripts I'm using for the Inventory and the shop:

Shop Manager

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

public class ShopManager : MonoBehaviour
{

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


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

        //ID's
        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;

        //Price
        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("Events").GetComponent<EventSystem>().currentSelectedGameObject;

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

            Item yourItem = new Item(); //get your Item from ID

            GameEvents.gameEvents.ItemPurchaseMade(yourItem);
        }


    }
}

GameEvents Script:

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

public class GameEvents : MonoBehaviour
{
    public static GameEvents gameEvents;

    private void Awake()
    {
        gameEvents = this;
    }

    public event Action<Item> onItemPurchase;

    public void ItemPurchaseMade(Item ItemID)
    {
        if (onItemPurchase != null)
        {
            onItemPurchase(ItemID);
        }
    }
}

Inventory Script:

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

public class Inventory : MonoBehaviour
{

    private List<Item> itemList;

    public void Start()
    {
        itemList = new List<Item>();

        GameEvents.gameEvents.onItemPurchase += AddItem;

        Debug.Log("Inventory");
    }

    public void AddItem(Item item)
    {
        itemList.Add(item);
    }
}

Even when i remove Debug.Log it still shows nothing.

0 Answers
Related