You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity游戏内购买物品未显示在Inventory中的问题求助

Unity商店购买物品后未显示在背包中的问题解决

我在Unity游戏的商店菜单中购买物品后,物品并未显示在Inventory(背包)中。金币已正常扣除,但背包始终为空。

相关截图

  • 打开商店菜单购买物品:打开商店菜单购买物品
  • 购买物品后金币扣除:购买物品后金币扣除
  • 购买后空Inventory菜单:购买后空Inventory菜单

使用的相关脚本

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);
    }
}

问题根源与修复方案

1. 核心问题点

  • ShopManager传递空Item:Buy()方法里new Item()创建的是无属性的空对象,既没有关联商店中对应ID的物品信息,后续即使添加到背包也无法正确显示。
  • Inventory无UI更新逻辑:itemList添加物品后,没有任何代码将数据同步到背包UI,导致视觉上看不到物品。
  • 潜在的事件订阅风险:若GameEvents对象初始化晚于Inventory,会导致订阅失败,需确保单例对象存在。

2. 具体修复步骤

(1)修正ShopManager的Buy方法,生成有效Item实例

根据你的Item类结构,给创建的Item赋值对应属性:

public void Buy()
{
    GameObject ButtonRef = GameObject.FindGameObjectWithTag("Events").GetComponent<EventSystem>().currentSelectedGameObject;
    int itemId = ButtonRef.GetComponent<ButtonInfo>().ItemID;

    if (coins >= shopItems[2, itemId])
    {
        coins -= shopItems[2, itemId];
        CoinsTXT.text = "Coins:" + coins.ToString();

        // 创建对应ID的Item实例,这里假设Item类有id、name等属性
        Item yourItem = new Item();
        yourItem.id = shopItems[1, itemId];
        // 可补充其他属性,比如从配置表加载名称、图标等

        GameEvents.gameEvents.ItemPurchaseMade(yourItem);
    }
}
(2)给Inventory添加UI刷新逻辑

假设背包有物品父容器和UI预制件,修改Inventory脚本:

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

public class Inventory : MonoBehaviour
{
    private List<Item> itemList;
    public Transform itemContainer; // 拖拽背包物品的父物体到Inspector
    public GameObject itemUIPrefab; // 物品UI预制件

    void Start()
    {
        itemList = new List<Item>();
        
        // 确保GameEvents单例存在再订阅
        if (GameEvents.gameEvents != null)
        {
            GameEvents.gameEvents.onItemPurchase += AddItem;
        }
        else
        {
            Debug.LogError("场景中不存在GameEvents实例!");
        }
    }

    public void AddItem(Item item)
    {
        itemList.Add(item);
        // 生成物品UI
        GameObject newItemUI = Instantiate(itemUIPrefab, itemContainer);
        // 根据Item属性设置UI,示例:
        // newItemUI.GetComponent<Image>().sprite = item.icon;
        // newItemUI.GetComponentInChildren<Text>().text = item.name;
    }
}
(3)验证基础配置
  • 确保GameEvents脚本挂载在场景中不会被销毁的对象上(如Manager物体)。
  • 检查商店按钮上的ButtonInfo组件,确认ItemID与shopItems中的索引匹配。
  • 在Inspector中给Inventory的itemContainer和itemUIPrefab赋值正确的对象。

内容的提问来源于stack exchange,提问作者kylorius beno

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 06:18:24