Possible Infinite Loop Freezing Game

I think this script “Card” is freezing my game every time I hit play by getting stuck in an infinite loop. I can’t figure out why this is the case (if it is) as it seems like the loop should close. Maybe I just need a fresh set of eyes to look at it since I have been staring at it too long.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Card : MonoBehaviour
{
public float cardValue;
public string suit;

public GameObject cardAvailability;

void Start()
{
    Debug.Log("Instantiating Card");

    cardAvailability = GameObject.FindGameObjectWithTag("CardAvailability");
    bool searching = true;

    while (searching)
    {
        cardValue = (int)(Random.Range(2, 15));
        float randomSuit = (int)(Random.Range(0, 4));
        
        if (cardAvailability.GetComponent<CardAvailability>().CheckCard(suit, cardValue) == true)
        {
            cardAvailability.GetComponent<CardAvailability>().disableCard(suit, cardValue);
            searching = false;
        }
    }

    Debug.Log("Card Instantiated: " + cardValue + suit);
}

public override string ToString()
{
    string output = "";

    switch (cardValue)
    {
        case 2:
            output += "Two";
            break;
        case 3:
            output += "Three";
            break;
        case 4:
            output += "Four";
            break;
        case 5:
            output += "Five";
            break;
        case 6:
            output += "Six";
            break;
        case 7:
            output += "Seven";
            break;
        case 8:
            output += "Eight";
            break;
        case 9:
            output += "Nine";
            break;
        case 10:
            output += "Ten";
            break;
        case 11:
            output += "Jack";
            break;
        case 12:
            output += "Queen";
            break;
        case 13:
            output += "King";
            break;
        case 14:
            output += "Ace";
            break;
        default:
            output += "Error";
            break;
    }
    output += " of " + suit + "s";
    return output;
}

}

public class CardAvailability : MonoBehaviour
{
//True means the card is available, false means it is unavailable

public bool[] hearts;
public bool[] diamonds;
public bool[] clubs;
public bool[] spades;

void Start()
{
    for(int i = 0; i <= 12; i++)
    {
        hearts *= true;*

diamonds = true;
clubs = true;
spades = true;
}
}
public bool CheckCard(string suit, float cardValue)
{
switch (suit)
{
case “hearts”:
if(hearts[(int)(cardValue - 2f)] == true)
{
return true;
}
else
{
return false;
}
case “diamonds”:
if (diamonds[(int)(cardValue - 2f)] == true)
{
return true;
}
else
{
return false;
}
case “clubs”:
if (clubs[(int)(cardValue - 2f)] == true)
{
return true;
}
else
{
return false;
}
case “spades”:
if (spades[(int)(cardValue - 2f)] == true)
{
return true;
}
else
{
return false;
}
}
Debug.Log(“No cases hit”);
return false;
}
public void disableCard(string suit, float cardValue)
{
if(suit == “hearts”)
{
hearts[(int)(cardValue - 2.0f)] = false;
}
if (suit == “diamonds”)
{
diamonds[(int)(cardValue - 2.0f)] = false;
}
if (suit == “clubs”)
{
clubs[(int)(cardValue - 2.0f)] = false;
}
if (suit == “spades”)
{
spades[(int)(cardValue - 2.0f)] = false;
}
}
}

The while loop is causing the game to freeze. Everything will stall during that while loop so it will never complete because the things you are waiting for will not be processed. If you want to use a while loop in Unity, use it in a coroutine:


void Start(){
    StartCoroutine(WaitForSomething());
}

IEnumerator WaitForSomething(){
    while(true){
        yield return null;
        
        if(someCondition){
            break;
        }
    }
}