首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >需要有关Unity的帮助:当前构造上下文中不存在参数

需要有关Unity的帮助:当前构造上下文中不存在参数
EN

Stack Overflow用户
提问于 2019-04-09 08:29:41
回答 1查看 108关注 0票数 0

我本打算练习如何创建清单,但它似乎不会读取其他脚本中的参数。我正在根据我的教程进行培训,代码如下:

代码语言:javascript
运行
AI代码解释
复制
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 () 
    {
    }
}

下面是另一个脚本:

代码语言:javascript
运行
AI代码解释
复制
[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;
    }
}

我没有期望任何东西,因为我还没有完成教程。有人知道为什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-09 08:46:18

好的在

代码语言:javascript
运行
AI代码解释
复制
InventoryCatalogue.Instance.AddMaterialToCatalogue(new ItemStack(FoodMaterial, amount));

你打电话给

代码语言:javascript
运行
AI代码解释
复制
new ItemStack(FoodMaterial, amount)

使用参数FoodMaterial, amount,但您的ItemCatalogue类或方法GetItem包含任何具有这些名称的变量/字段/属性。

您必须在其中传递一些值,例如

代码语言:javascript
运行
AI代码解释
复制
// I don't know what you want to pass in as amount
new ItemStack(item, 1)

所以

代码语言:javascript
运行
AI代码解释
复制
Items item = AvailableItems[Random.Range(0, AvailableItems.Length)];
InventoryCatalogue.Instance.AddMaterialToCatalogue(new ItemStack(item, 1));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55588303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档