Usign Exists on a List not working on second trip

Hey all, I have a small routine that check if an entry exists:

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;
        }

    }

It works perfect the first time if you select “Fish Tank” as many tiumes as you want, however, if you select “Grow Bed” after that and then go back and select “Fish Tank” again, it fails and goes to the else statement to create a new text.
Why does it only work the first time?
Thanks in advance.

That’s probably not a very wise way to organize or access your data, but I don’t think that’s causing the particular problem you asked about.

I would guess the problem lies in your make_cart_text function, which you haven’t posted. Specifically, I’d guess that you are somehow accidentally removing the fish tank from cart_array when you add the grow bed.

Sorry, I’m new to Unity and even C#, but everyone seems to like it better than java, but anyway, here is the make_cart_text,

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);
        print_cart ();
        fillCartTxt(cart_count);
    }

But, i do show the the cart is not being overwritten, cart_count is accurate, my point is to add a new record only if the product is not in the array already, if it is, just increment the amount.
Again, sorry about my newbness.

Well, you said in your original post that if you click on fish tank after clicking on grow bed, it executes the “else” clause and makes a new text.

That means that “if(cart_array.Exists(cart => cart.name == “Fish Tank”) )” must have evaluated to FALSE. Because otherwise the code in the “else” clause would not have been executed.

So if the information you provided is accurate, something must have happened to make that condition false.

Well, i think you are on to something here, i have been trying to debug.log the cart_array, then realized i set it to pulic, and sure enough, when it write to the list:
cart_array.Add(mycart);
it over write ALL the records in the array to the current mycart.
i dont understand, if im using Add, why is it updating all the other records?
Thanks a ton.
Jeff

The cart_array is basically a list of references to carts, since “cart” is a class and therefore a “reference type”. If you have one variable, “my_cart”, and you’re always setting and changing the values on that, you only have one actual cart and you’re just filling the list with multiple references to that one cart. Instead you should be doing something like “var cart = new Cart()” every time you add a new cart to the list. That way you have multiple instances of carts, and each entry in the list will be pointing to a separate cart, rather than all of them pointing to the same cart.

ok, seems to be all i had to do was give it: mycart = new cart (); before i built the Add.
Thanks for all the help.
Jeff