我将练习如何创建库存,但似乎不会从其他脚本读取参数。我正在根据我的教程进行培训,代码如下:
public class ItemCatalogue : MonoBehaviour
{
public Items[] AvailableItems;
public Text DisplayArray;
public void GetItem()
{
Items item = AvailableItems[Random.Range(0, AvailableItems.Length)];
InventoryCatalogue.Instance.AddMaterialToCatalogue(new ItemStack(FoodMaterial, amount));//weird, the parameter doesn't exist
DisplayArray.text = item.name;
}
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
}
这是另一个脚本:
[System.Serializable]
public class ItemStack
{
public Items FoodMaterial;
public int amount;
public ItemStack(Items FoodMaterial, int amount) //here's the parameter
{
this.FoodMaterial = FoodMaterial;
this.amount = amount;
}
}
我没想到什么,因为我还没有完成本教程。有人知道为什么吗?
答案 0 :(得分:1)
进去
InventoryCatalogue.Instance.AddMaterialToCatalogue(new ItemStack(FoodMaterial, amount));
您致电
new ItemStack(FoodMaterial, amount)
具有参数FoodMaterial, amount
,但您的ItemCatalogue
类或方法GetItem
包含具有这些名称的任何变量/字段/属性。
您宁愿在其中传递一些值,例如
// I don't know what you want to pass in as amount
new ItemStack(item, 1)
如此
Items item = AvailableItems[Random.Range(0, AvailableItems.Length)];
InventoryCatalogue.Instance.AddMaterialToCatalogue(new ItemStack(item, 1));