Creating card game - copy of list to new list - creating cycle

Hi, I am new in Unity and I am trying to make a card game (not classic cards).

I have a deck from 40 cards. 10 cards are added to one panel, 3 cards to another one and then 27 left are displayed in another panel on button press (player see only one, but it’s opening 3 cards). Everything works perfect, but when those 27 cards ends I need them to go back to the same list that player could press button and see it again that it would work as a cycle.

I was trying to create one more list and add those there, but the result is that after the last card on button press it shows only that one (since the function has to open three cards at one press so it opens the same card three times).
This is my code. Does someone have an idea how can I fix it?

public GameObject number; //for number of deck array
	
public List<GameObject> deck = new List<GameObject>();
private List<GameObject> displayed = new List<GameObject>();

//it's code only for those 27 cards that left
public void OpenThreeCards(GameObject panel)
	{
		for (int i = 0; i < 3; i++) {
			
			int number = deck.Count - 1;

			if (number == 0) {
				deck = displayed;
			}

			GameObject k;
			int r;
			r = 0;
			k = Instantiate (deck [number]);
			k.transform.SetParent (panel.transform);
		
			k.GetComponent<RectTransform> ().localRotation = Quaternion.identity;
			displayed.Add (deck [number]);
			deck.Remove (deck [number]);
		}

	}

I’m not sure whether i understand what you want, so I’m going to interpret your explanation as best as I can :stuck_out_tongue:

You have 2 lists, deck and displayed.

  1. At start, deck has 27 cards, displayed has 0 cards.
  2. At the end displayed has 27 cards, deck has 0 cards.

When 2 hapens you want to go back to 1.

You currently call deck = displayed, I suppose you do that in an effort to achieve this. However, the first thing thats wrong with this is that List is a reference type, not a value type. When you set deck to displayed. They are from then on the same object (not just 2 objects with the same value). Which means that changes to deck are also applied to displayed and vise versa. you probably want to do deck = new List(displayed) instead. As this would create a new list with the same values. Secondly, you do this before you do something with the last card. I’m assuming this is not intentional, not sure though :stuck_out_tongue:

I think you want to do something like this:

public void OpenThreeCards(GameObject panel)
{
    // assumes amount of cards (27) can be devided by 3 (3, 6, 9, 12, etc.)
    for (int i = 0; i < 3; i++)
    {
        int number = 0; // first instead of last to keep displayed in the same order as deck

        GameObject k;
        k = Instantiate(deck[number]);
        k.transform.SetParent(panel.transform);

        k.GetComponent<RectTransform>().localRotation = Quaternion.identity;
        displayed.Add(deck[number]);
        deck.Remove(deck[number]);
    }
    if (deck.Count == 0)
    {
        deck = new List<GameObject>(displayed);
        displayed.Clear();
    }
}

Another thing you might want to consider for efficiency is using 2 Queues instead Lists.