Unity List 只存在第一遍

Unity List Exists only first pass

我有一个检查条目是否存在的小例程:

public class cart {public string name; public int cost; public int amt;}
public string[,] prod_Array = new string[6,2];
public List<cart> cart_array = new List<cart>();
public cart mycart = new cart ();
public void buyProduct (string product) {
    string item = "";
    switch (product) {
    case "fishTank":
        cost = int.Parse(prod_Array[5,1]);
        if(cart_array.Exists(cart => cart.name == "Fish Tank") ){
            cart qty = cart_array.Find(cart => cart.name == "Fish Tank");
            qty.amt= qty.amt+1;
            fillCartTxt(cart_count);
        } else {
            Debug.Log("None found");
            item = "Fish Tank";
            make_cart_text(item, cost);
        }
        break;
    case "growBed":
        cost = int.Parse(prod_Array[4,1]);
        if(cart_array.Exists(cart => cart.name == "Grow Bed")){
            cart qty = cart_array.Find(cart => cart.name == "Grow Bed");
            qty.amt= qty.amt+1;
            fillCartTxt(cart_count);
        } else {
            item = "Grow Bed";
            make_cart_text(item, cost);
        }
        break;
    }

如果您 select "Fish Tank" 多次就可以完美运行,但是,如果您 select "Grow Bed" 之后再返回和 select "Fish Tank" 再次失败并转到 else 语句以创建新文本。 为什么它只在第一次起作用? 忘了添加:

void make_cart_text(string item, int cost){
    mycart.name = item;
    mycart.cost = cost;
    mycart.amt = 1;
    int ny = 0;
    cart_array.Add(mycart);
    cart_count = cart_array.Count;
    if (cart_count == 1) {
        ny =50;
    } else if (cart_count > 1) {
        ny = cart_count * 20;
    }
    GameObject cart_line = GameObject.Instantiate(
        Resources.Load("cart_line"),
    new Vector3(19f,ny,0),Quaternion.identity)as GameObject;
    cart_line.name= "carttext"+cart_count;
    GameObject gc2 = GameObject.Find ("Cart");
    cart_line.transform.SetParent(gc2.transform, false);
    fillCartTxt(cart_count);
}

刚刚添加mycart = new cart();在 add 语句使其工作之前的 make_cart_text 的第一行。 感谢所有的帮助。 杰夫