Infinite loop might be freezing game

I think this script “Card” is instantly 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;
}
}
}

Using a while loop in the start method will freeze the game. You should be using a Coroutine:

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

IEnumerator WaitForStuff(){
    while(true){
        yield return null;
        if(stuffHasCompleted){ break; }
    }
}

That right there will not block the main thread. Everything in Unity relies on the main thread, so the stuff your waiting on in the Start method, will likely never complete and that is why you have the issue because the main thread is stuck in your loop. A couroutine is perfect in this case, because you just need to check if the stuff has completed at the end of every update loop and if it hasn’t, then just let Unity do it’s thing until it has.

Hope that makes sense

That makes perfect sense, thanks so much!