IEnumerator Freezing The Game

IEnumerator BoxSpawning(DataForBoxSpawning dataForBoxSpawning)
 {
     GameObject box = Instantiate(dataForBoxSpawning.TEST_BoxPrefab, dataForBoxSpawning.TEST_BoxSpawnPos.position, dataForBoxSpawning.TEST_BoxSpawnPos.rotation, null);
     currentBox = box;
     if (box == null)
     {
         Debug.LogError("Box prefab oluşturulamadı! Box null döndü.");
         yield break;
     }

     Rigidbody rb = box.GetComponent<Rigidbody>();

     Box b = box.transform.Find("Box")?.GetComponent<Box>();
     if (b == null)
     {
         Debug.LogError("Box bileşeni bulunamadı!");
         yield break;
     }

     rb.useGravity = false;
     rb.isKinematic = true; // Box donduruldu

     var senderType = BoxSenderTypeRandomiser(gameHandler.Day);
     b.boxSenderType = senderType;
     if (senderType == null)
     {
         Debug.LogError("BoxSenderTypeRandomiser null döndürdü!");
         yield break;
     }

     var contentType = BoxContentTypeRandomiser(gameHandler.Day, senderType);
     b.boxContentType = contentType;
     if (contentType == null)
     {
         Debug.LogError("BoxContentTypeRandomiser null döndürdü!");
         yield break;
     }

     SpawningContentListRandomizer(contentType, dataForBoxSpawning);
     StopCoroutine(BoxSpawning(dataForBoxSpawning));
 }

I Dont Understand Why It Freezes 1 out of 10 runs, i am about to lose my mind

When Unity freezes permanently it’s usually an infinite loop that’s the culprit. The coroutine looks fine to me (although I’m not sure why you’re even using a coroutine for this?) so I’m guessing the issue lies in one of the randomization methods that are being called from it.

2 Likes

Well you are right actually, it was ment to be a coroutine but i deleted alot of things that i thought was making it freeze so now its not even a coroutine xd. Will turn it into a normal function thanks for reminding.

Well it fixed it. Lol i turned it into a normal function and saw the error. it seems like it couldnt reach a certain list that i was requesting from another script so the coroutine never stops after it started error was i misspelled “explosive” as “exsplosive”