Why does this freeze unity?

So i am making a match memory game and whenever i tried to run it it freezes and crashes?

Code

GameManager

Card

The freeze could be the while loop getting stuck in an infinite loop.

Depending on how many cards there are, how is the loop supposed to break if all the cards have been initialized? I’m not certain if this is the problem, I’m only guessing. but this snippet looks like it’s asking for trouble:

        for (int i = 1; i < 14; i++)
        {
            bool test = false;
            int choice = 0;
            while (!test)
            {
                choice = Random.Range(0, cards.Length);
                test = !(cards[choice].GetComponent<Card>().initialized);
            }
            cards[choice].GetComponent<Card>().cardValue = i;
            cards[choice].GetComponent<Card>().initialized = true;
        }

To be certain, you can add Debug.Log and output what card has been chosen and it’s properties.

I think the answer lies here:

test = !(cards[choice].GetComponent<Card>().initialized);

test = NOT (intialized).

This means when all cards are initialized (true), test will always be false NOT(true), and thus the loop will go on forever.

@PenguinLord