无法将项目添加到库存<list> - Unity C#

时间:2017-09-21 20:39:01

标签: c# unity3d add

我基本上遇到了这个问题,当我在inventory.Add(database.Items[16]);函数中尝试此代码Start()时,它确实会将项目添加到我的库存中,但每当我在buttonClick或任何其他函数上尝试此代码时,而不是Start(),它就像空气,它什么都不做......请求帮助

Inventory.cs

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

public class Inventory : MonoBehaviour
{
    public List<Item> inventory = new List<Item>();
    public List<Item> slots = new List<Item>();

    public GameObject slotPreset;
    public GameObject parentInvetoryGridForSlots;

    public Text itemNameText;

    private ItemDatabase database;

    void Start()
    {
        database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
        //Add item to invetory code - inventory.Add(database.Items[16]);

        for (int i = 0; i < inventory.Count; i++)
        {
            inventory.Add(new Item());
        }

        AddItem(1);
        AddItem(2);
        AddItem(3);
        AddItem(4);


        for (int i = 0; i < inventory.Count; i++)
        {
            GameObject spawnedSlot = Instantiate(slotPreset);
            spawnedSlot.transform.SetParent(parentInvetoryGridForSlots.gameObject.transform);

            itemNameText.text = inventory[i].itemName;
        }
    }

    void RemoveItem(int id)
    {
        for (int i = 0; i < inventory.Count; i++)
        {
            if (inventory[i].itemID == id)
            {
                inventory[i] = new Item();
                break;
            }
        }
    }

    void AddItem(int id)
    {
        for (int i = 0; i < inventory.Count; i++)
        {
            if (inventory[i].itemName == null)
            {
                for (int j = 0; j < database.Items.Count; j++)
                {
                    if (database.Items[j].itemID == id)
                    {
                        inventory[i] = database.Items[j];
                    }
                }
                break;
            }
        }
    }
}

Item Database.cs

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

public class ItemDatabase : MonoBehaviour {
    public List<Item> Items = new List<Item>();

    //Edit missing ItemType3 colors... Or wrong colors... And add missing colors to database...

    void Start()
    {
        //Wanderer Crate
        Items.Add(new Item("Bloody Combat Pants", 0, 0.12f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Pants, Item.ItemType3.Gray));
        Items.Add(new Item("Blue Hi-top Trainers", 1, 2.12f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shoes, Item.ItemType3.Green));
        Items.Add(new Item("Boots (Punk)", 2, 0.19f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shoes, Item.ItemType3.Green));
        Items.Add(new Item("Cargo Pants (Beige)", 3, 0.19f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Pants, Item.ItemType3.Gray));
        Items.Add(new Item("Cargo Pants (Blue)", 4, 0.04f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Pants, Item.ItemType3.Gray));
        Items.Add(new Item("Cargo Pants (Khaki)", 5, 0.06f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Pants, Item.ItemType3.Gray));
        Items.Add(new Item("Dirty Tank-top (White)", 6, 0.04f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shirt, Item.ItemType3.Gray));
        Items.Add(new Item("Floral Shirt", 7, 10.19f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shirt, Item.ItemType3.Blue));
        Items.Add(new Item("Glasses (Punk)", 8, 0.37f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Glasses, Item.ItemType3.Green));
        Items.Add(new Item("Hotpants (Blue)", 9, 138.12f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Pants, Item.ItemType3.Purple));
        Items.Add(new Item("Long Sleeved T-shirt (Striped)", 10, 0.04f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shirt, Item.ItemType3.Gray));
        Items.Add(new Item("Long-sleeved T-shirt (Black)", 11, 0.04f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shirt, Item.ItemType3.Gray));
        Items.Add(new Item("Long-sleeved T-shirt (Light Blue)", 12, 0.04f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shirt, Item.ItemType3.Gray));
        Items.Add(new Item("Mandarin Jacket (Blue)", 13, 18.60f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Jacket, Item.ItemType3.Violet));
        Items.Add(new Item("Padded Jacket(Urban)", 14, 1.77f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Jacket, Item.ItemType3.Blue));
        Items.Add(new Item("Pleated Mini-skirt (Blue)", 15, 170.97f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Pants, Item.ItemType3.Red));
        Items.Add(new Item("Ballistic Mask", 16, 19.02f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Mask, Item.ItemType3.Violet));
        Items.Add(new Item("Protective Glasses", 17, 45.43f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Glasses, Item.ItemType3.Purple));
        Items.Add(new Item("School Shirt (Open)", 18, 17.14f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shirt, Item.ItemType3.Violet));
        Items.Add(new Item("School Shoes", 19, 17.14f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shoes, Item.ItemType3.Blue));
        Items.Add(new Item("Working Boots", 20, 0.04f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shoes, Item.ItemType3.Gray));
        Items.Add(new Item("Slacks (Blue)", 21, 4.20f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Pants, Item.ItemType3.Blue));
        Items.Add(new Item("T-shirt (Red)", 22, 0.04f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shirt, Item.ItemType3.Gray));
        Items.Add(new Item("T-shirt (Striped)", 23, 0.04f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shirt, Item.ItemType3.Gray));
        Items.Add(new Item("Tank - top(Charcoal)", 24, 0.09f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shirt, Item.ItemType3.Gray));
    }

}

Opeed Item Seel Keep.cs(在此代码中'inventory.Add(database.Items [itemID]);'不起作用)

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

public class openedItemSellKeep : MonoBehaviour {

    public Text itemIDText;
    public Text moneyText;

    public GameObject openedCrateItemPanel;
    public GameObject backCoverPanel;

    private ItemDatabase database;

    public List<Item> inventory = new List<Item>();

    public void sellItem()
    {
        database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
        int itemID = Convert.ToInt32(itemIDText.text);
        double doubleMoney = Convert.ToDouble(File.ReadAllText("money.txt"));
        float money = ToSingle(doubleMoney);
        Debug.Log(database.Items[itemID].itemPrice);
        money += database.Items[itemID].itemPrice;
        moneyText.text = money.ToString() + "$";
        File.WriteAllText("money.txt", money.ToString());
        openedCrateItemPanel.SetActive(false);
        backCoverPanel.SetActive(false);
    }

    public void keepItem()
    {
        database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
        int itemID = Convert.ToInt32(itemIDText.text);
        inventory.Add(database.Items[itemID]);
        openedCrateItemPanel.SetActive(false);
        backCoverPanel.SetActive(false);


    }

    public static float ToSingle(double value)
    {
        return (float)value;
    }
}

1 个答案:

答案 0 :(得分:0)

您的每个功能都在创建Inventory对象的新实例。如果您希望多个脚本可访问的数据库始终是同一个实例,请考虑将其设置为静态和全局并创建一次。还要将所有内容添加到同一列表中。我假设您的命名惯例是单个玩家的库存?

如果您需要多个玩家的多个数据库实例,那么只需创建一次并为其保留参考。

最后一个建议。看起来您使用昂贵的查找来检索信息和删除项目。考虑使用将ID用作键并将Item用作值的字典。这样你就不用去搜索了 对于每个项目。

void AddItem(int id)
{        
    inventory[id] = database[id];
}

void RemoveItem(int id)
{
    inventory.Remove(id);
}