My code is frezzing Unity and I don't know why.

Here’s he code.
The idea is that I am selecting random place in circle and checking if I can spawn my prefab there, but the problem is that Unity frezes randomly. This not being consistant (It can happen at any point of the code) and frezing makes me unable to find out easly what’s going on . Please send help!

 if (nutrition > 400f)
        {
            bool canSpawn = false;
            Vector3 pos = transform.position;
            int tries = 0;
            Quaternion rot = Quaternion.identity;
            rot.y = Quaternion.Euler(0, Random.Range(0f, 360f), 0).y;
            List<GameObject> foods = new List<GameObject>();
            while (!canSpawn || tries > 10)
            {
                float r = UnityEngine.Random.Range(0f, Mathf.PI * 2);
                Vector3 randomPos = new Vector3(Mathf.Sin(r), 0, Mathf.Cos(r));
                float randomRadius = UnityEngine.Random.Range(0f, 3f);
                pos = transform.position + (randomPos * randomRadius);
                pos.y = 0f;
                RaycastHit[] hits = new RaycastHit[100];
                Physics.BoxCastNonAlloc(pos, Vector3.one, transform.forward,hits, rot, 1f);

                for (int i = 0; i > hits.Length; i++)
                {
                    if (hits*.collider.gameObject.layer == LayerMask.NameToLayer("Food"))*

{
foods.Add(hits*.collider.gameObject);*
}
}
if (foods.Count == hits.Length)
{
canSpawn = true;
}
else
{
foods.Clear();
}
tries++;
Debug.Log("Try number: " + tries.ToString());
}
if (canSpawn)
{
foreach (GameObject o in foods)
{
GameObject.Destroy(o);
foods.Remove(o);
nutrition += 5;
}
string objectName = “Name”;
string folderpPath = Path.Combine(“Assets”, “Creatures”, objectName);
string path = Path.Combine(folderpPath, “DataOf” + objectName + “.asset”);
GameObject gamePrefab = PrefabUtility.LoadPrefabContents(Path.Combine(folderpPath,
“PrefabOf” + objectName + “.prefab”));
GameObject newCreatue = Instantiate(gamePrefab, pos, rot);
Creature creature = newCreatue.AddComponent();
creature.timeToLive = 500f;
creature.network = network;
creature.timeFamilySurvived = timeFamilySurvived;
creature.foodGathered = foodGathered;
creature.reproduced = reproduced;
creature.distanceTraveled = distanceTraveled;
creature.sensorMask = sensorMask;
creature.nutrition = 300f;
creature.distanceTraveled = 0f;
creature.collided = false;//To tell if the car has crashed
creature.creatureInfo = newCreatue.GetComponent();
nutrition -= 200f;
reproduced++;
}
else // I just gave up over here and tried to Instantize it anyway but it still randomly crashes.
{
string objectName = “Name”;
string folderpPath = Path.Combine(“Assets”, “Creatures”, objectName);
string path = Path.Combine(folderpPath, “DataOf” + objectName + “.asset”);
GameObject gamePrefab = PrefabUtility.LoadPrefabContents(Path.Combine(folderpPath,
“PrefabOf” + objectName + “.prefab”));
pos.y += 2;
GameObject newCreatue = Instantiate(gamePrefab, pos, rot);
Creature creature = newCreatue.AddComponent();
creature.timeToLive = 500f;
creature.network = network;
creature.timeFamilySurvived = timeFamilySurvived;
creature.foodGathered = foodGathered;
creature.reproduced = reproduced;
creature.distanceTraveled = distanceTraveled;
creature.sensorMask = sensorMask;
creature.nutrition = 300f;
creature.distanceTraveled = 0f;
creature.collided = false;//To tell if the car has crashed
creature.creatureInfo = newCreatue.GetComponent();
nutrition -= 200f;
reproduced++;
}
}

Its the while loop. If you’re freezing in a area of code, and you have a while loop there, it is almost always going to be the while loop’s fault.

In your case you have the tries comparison backwards.

while (!canSpawn || tries > 10)

If your loop does not find a spot in 11 tries, that loop will be infinite, even if it does find one after.

What you want is:

while(!canSpawn && tries < 10).

This will try 10 times, and if it still hasn’t found a spot, it will give up.