Help: Unity keeps Freezing....

Hello everyone. I am trying to make a simple game with spawners etc. and recently after altering some code, Unity seems to freeze completely. Not only the game but the editor too. Could anyone tell me what’s the problem?

using System.Collections;
using UnityEngine;

public class Spawner : MonoBehaviour
{
    //public AudioClip DieSoundeffect;
    //public GameObject explosion;

    public GameObject[] enemies;
    public Vector3 spawnValues;
    public float spawnWait;
    public float spawnMostWait;
    public float spawnLeastWait;
    public int startWait;

    public int rendEnemy;

    public int kill = 0;
    public int notkill = 0;
    public int count;
    public int KillCounter;
    public int KillMax;
    public int KillLeast;
    public int counter_max;

    private int kill_max_spawn = 5;
    private int notkill_max_spawn = 3;

    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(WaitSpawner());
        while (kill < kill_max_spawn)
        {
            Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));
            Instantiate(enemies[0], spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);
            kill++;
            KillCounter++;
        }
        while (notkill < notkill_max_spawn)
        {
            Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));
            Instantiate(enemies[1], spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);
            notkill++;
            KillCounter++;
        }
    }

    // Update is called once per frame
    void Update()
    {
        spawnWait = Random.Range(spawnLeastWait, spawnMostWait);
    }

    IEnumerator WaitSpawner()
    {
        yield return new WaitForSeconds(startWait);

        while(true)
        {
            count = Random.Range(KillLeast, KillMax);//8,9 notkill - 1, kill - 0

            while (KillCounter < counter_max)
            {
                rendEnemy = Random.Range(0, 2); //0,1

                if (notkill >= kill)
                {
                    rendEnemy = 0;
                }
                if (kill >= count)
                {
                    //rendEnemy = 1;
                }
                Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));
                Instantiate(enemies[rendEnemy], spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);
                KillCounter++;
                if (rendEnemy == 0)
                {
                    kill++;
                }
                else
                {
                    notkill++;
                }
                yield return new WaitForSeconds(spawnWait);
            }
        }
    }
}

Probably because the first while loop in the WaitSpawner() method isn’t using a yield each iteration. Only the inner while loop is using yield.

Your startWait is 0. So it will wait zero seconds.

Edit scratch that, I see it’s public you probably set it from the editor. Yeah, the outer loop also can be a problem. Really you shouldn’t build infinite loops in Unity. The playerloop is built for this purpose.

BTW, your abusing both the while loop and the coroutines, it’s highly advisable that you should refactor this whole mess into more manageable pieces.

I avoid while loops like the plague. I only use them when absolutely necessary. And especially for the type of code you are using, a for loop would be more appropriate. Perhaps a bit more verbose, but a little extra code is well worth avoiding the risks that while loops introduce.

It also seems as though you are going for a “pooling” approach, but aren’t actually using a pool. I would advise defining a finite collection of entries to use, and drawing from that collection for “spawning.” It is easier and safer to create a collection of objects, and draw from that collection, than it is to create and destroy over and over.

While loop on line 59 is going to run forever once KillCounter >= counter_max, never letting anything else in your game run, even the editor itself. When using loops you need to ensure you can always get out of the loop eventually. I would never use “while(true)” in Unity, as you are just asking for this type of problem.