(C#)在条件下实例化,然后重用

时间:2018-11-28 15:14:55

标签: c# unity3d instantiation

我对编程非常陌生,请耐心等待。

public void clickWood()
{
   //Check If The Player Has Gotten Wood Previously
   if(_hasWood == false)
    {

        GameObject newItem = Instantiate(wood_prefab) as GameObject;

        //Initial Displayed Name
        newItem.GetComponent<ButtonSetter>().setName("WOOD: ");

        //Starts with 0 Wood, set to 1
        newItem.GetComponent<ButtonSetter>().item_count += 1;

        newItem.transform.SetParent(GameObject.Find("Content").transform,false);

        //Got their first wood, should run else from now on
        _hasWood = true;
    }
    else
    {
        //SEE CONTINUED POST
    }
}

所以,在else语句中,我基本上要说,

他们获得了第一批木材,因此我们创建了一个面板来显示有关他们拥有的木材的信息。现在我要说的是,由于我们已经实例化了displayPanel,所以我们只想从中进行工作并调整对象变量,以控制“ Wood:0”的整数部分

应该是

newItem.GetComponent<ButtonSetter>().item_count

但是,如果我尝试在else语句中访问它,如图所示:

newItem.GetComponent<ButtonSetter>().item_count += 1;

它告诉我newItem在当前上下文中不存在。 如何在特定条件下实例化某些东西,然后在满足条件后,获取实例化的对象并使用其脚本/变量?

Visual representation of my issue

2 个答案:

答案 0 :(得分:2)

我认为您需要对代码进行一些重组。

这就是我要怎么做

public class Item : MonoBehaviour {
    public string itemName;
    public int count;
}

我将为木材,铁或您的游戏中存在的任何东西创建一个基础类Item

接下来,我将创建一个处理程序脚本,该脚本跟踪所有被单击/未被单击的项

Handler.cs

公共类处理程序:MonoBehaviour {

public Item itemPrefab;

public Dictionary<string, Item> displayItems= new Dictionary<string, Item>();

public void OnWoodClicked()
{
    OnItemClicked("wood");
}

public void OnIronClicked()
{
    OnItemClicked("iron");
}

private void OnItemClicked(string itemKey)
{
    if (displayItems.ContainsKey(itemKey))
    {
        displayItems[itemKey].count++;
    }
    else
    {
        Item item = Instantiate(itemPrefab);
        item.count = 1;
        item.itemName=itemKey;
        displayItems.Add(itemKey, item);

    }
}
}

因此,为了跟踪创建的项目,我创建了一个公开的Dictionary<string, Item> displayItems;字典

在此脚本OnItemClicked(string itemKey)中,方法将检查是否已创建此类型的项目。 (通过检查该键是否存在)

如果未创建该项目,则我们将实例化新的Item(要显示的显示项目的预制件),然后根据其键将其添加到字典中。

但是,如果该对象已经存在,则可以根据需要以displayItems[itemKey]的身份访问该对象

例如,如果您单击“木材”,则可以访问displayItems["wood"]作为木材展示项目。

我希望这会有所帮助。 如果您想让我说的更清楚,请发表评论。

答案 1 :(得分:1)

目前,newItem仅位于代码的“ IF”子句中,因此“ ELSE”子句看不到它。

如果您将其分配给类级别的字段(就像您在类级别上分配了_hasWood一样),则可以在“ IF”或“ ELSE”块中对其进行访问,并保持计数在对象的整个生命周期中都会从中调用“ clickWood()”。

private bool _hasWood;
private GameObject _woodItem;

public void clickWood()
{
    //Check If The Player Has Gotten Wood Previously
    if(_hasWood == false)
    {

        _woodItem= Instantiate(wood_prefab) as GameObject;

        //Initial Displayed Name
        _woodItem.GetComponent<ButtonSetter>().setName("WOOD: ");

        //Starts with 0 Wood, set to 1
        _woodItem.GetComponent<ButtonSetter>().item_count += 1;

        _woodItem.transform.SetParent(GameObject.Find("Content").transform,false);

        //Got their first wood, should run else from now on
        _hasWood = true;
    }
    else
    {
        _woodItem.GetComponent<ButtonSetter>().item_count += 1;
    }
}