While Loop Freezing Unity

What’s wrong with this code? Whenever OnMouseDown is called unity freezes.

public class createTroops : MonoBehaviour {
    public stats statsVars;
    public GameObject prefab;

    public int incPos = 4;

    void OnMouseDown()
    {
        while (statsVars.units <= statsVars.units + statsVars.units)
        {
            Instantiate(prefab, new Vector3(-23.3f, 10 - incPos, 0), Quaternion.identity);
            statsVars.units++;
        }
        if (statsVars.units >= statsVars.units + statsVars.units)
        {
            statsVars.units = 0;
        }
    }
}

The condition of your while loop is that a number is less than or equal to twice itself, which is true for every non-negative number. This causes your loop to run forever. Since it’s doing it in the main thread, that causes Unity to appear to freeze.

I feel like what you maybe want to do is create statsVars.units prefabs? If so, make a separate counter variable to be incremented and tested against the upper boundary. That will let you break out of the loop.