# Any suggestions for implementing progressive difficulty

Hello everyone. I created a simple 2D shooter game a while back in Flash. I used a variable called totalEnemies = 10; and at the beginning of each level I would do totalEnemies++; This was all fine and dandy when the object of the game was simply to eliminate all enemies. My Unity game however, spawns enemies continuously until X amount of cash is collected. The enemies act as kind of obstacles. With each level I want to increase the number of enemies. I’ve tried numerous things, I won’t paste all my versions of this statement, but I’ve tried implementing a sort of totalEnemies++; but depending on how I implemented it it either didn’t work or froze Unity completely. Any suggestions? I feel like I coded myself into a wall.

``````function LevelEnd(){
while (isSpawningEnemies) {
if(level <=5){
posX = Random.Range(-4.213592,4.336067);

yield WaitForSeconds(3);
Instantiate(enemies, Vector3(posX, 0.05523491, 3.545458),transform.rotation);
totalEnemies++;
}

if(level >= 6){
posX = Random.Range(-4.213592,4.336067);

yield WaitForSeconds(2);
Instantiate(enemies, Vector3(posX, 0.02, 3.545458),transform.rotation);
totalEnemies++;
}

if(level >= 10){
posX = Random.Range(-4.213592,4.336067);

yield WaitForSeconds(3);
Instantiate(enemies, Vector3(posX, .02, 3.545458),transform.rotation);
}
}
}
``````

**EDIT

``````function LevelEnd(){
while (isSpawningEnemies) {
if(level <=5 && totalEnemies < level+1){
posX = Random.Range(-4.213592,4.336067);

yield WaitForSeconds(3);
Instantiate(enemies, Vector3(posX, 0.05523491, 3.545458),transform.rotation);
totalEnemies++;
}

if(level >= 6 && totalEnemies < level){
posX = Random.Range(-4.213592,4.336067);

yield WaitForSeconds(2);
Instantiate(enemies, Vector3(posX, 0.02, 3.545458),transform.rotation);
totalEnemies++;
}

if(level >= 10 && totalEnemies < level){
posX = Random.Range(-4.213592,4.336067);

yield WaitForSeconds(3);
Instantiate(enemies, Vector3(posX, .02, 3.545458),transform.rotation);
}
}
}
``````

I notice your while loop breaks on `isSpawningEnemies`. If that value is true and never becomes false, your loop will never complete.

Here is what I ended up doing.

``````var isSpawningEnemies : boolean = true;
private var EnemiesNeeded:int;

function Awake(){
gameOver = false;
points = 0;
cash = 0;
lives = 3;
level = 1;
EnemiesNeeded = level;
}

function Update () {
isSpawningEnemies = false;
if(cash >= level * 200){
level++;
LevelEnd();
}

function LevelEnd(){
if(level <= 5){
isSpawningEnemies = true;
while(totalEnemies < EnemiesNeeded && isSpawningEnemies){
posX = Random.Range(-4.213592,4.336067);

yield WaitForSeconds(3);
Instantiate(enemies, Vector3(posX, .02, 3.545458),transform.rotation);
}
isSpawningEnemies = false;
}
if(level >= 6 && level <= 10){
isSpawningEnemies = true;
while(totalEnemies < EnemiesNeeded){
posX = Random.Range(-4.213592,4.336067);

yield WaitForSeconds(3);
Instantiate(enemies, Vector3(posX, .02, 3.545458),transform.rotation);
}
}
if(level == 11){
isSpawningEnemies = true;
EnemiesNeeded = 3;
while(totalEnemies < EnemiesNeeded){
posX = Random.Range(-4.213592,4.336067);

yield WaitForSeconds(3);
Instantiate(enemies, Vector3(posX, .02, 3.545458),transform.rotation);
}
}
}
``````

Seems to be working (fingers crossed)