# How to increase the game "level" after certain amount of objects spawned?

The game is supposed to increase the level by 1 every 10 spawns of an object, using totalShots % 10 == 0. So, when totalShots is equal to 10, 20, 30, etc. the level should increase by 1, however, the level keeps on increasing in the time between the 10th and 11th spawn (example: http://gfycat.com/ArtisticAdorableGreatwhiteshark).

This is the code:

var totalShots : int = 0;
var level : int = 0;

function Start () {

}

function Update () {
transform.Translate(Vector3(0, direction * speed * Time.deltaTime, 0));
if (Time.time > lastSpawn + nextSpawn){
Instantiate(shot, new Vector3(transform.position.x - 1, transform.position.y, transform.position.z), Quaternion.identity);
totalShots++;

lastSpawn = Time.time;
nextSpawn = Random.Range(2, 3);
}

}

function ShotCounter() {
if(totalShots % 10 == 0){
level++;
}
return(level);
}


Should this code be working or is there something that I am doing wrong? How can I make it so that the level only increases by 1?

You have a case where several frames meet the condition of (totalShots % 10 == 0). The best way to fix this would be to check if we need to increment the level only after we increment totalShots. This will also increase game performance by decreasing the frequency of checking for this condition to only after the respective variable is updated, instead of checking for this condition on every frame.

function Update () {
transform.Translate(Vector3(0, direction * speed * Time.deltaTime, 0));
if (Time.time > lastSpawn + nextSpawn){
Instantiate(shot, new Vector3(transform.position.x - 1, transform.position.y, transform.position.z), Quaternion.identity);
totalShots++;
if(totalShots % 10 == 0){
level++;
}

lastSpawn = Time.time;
nextSpawn = Random.Range(2, 3);
}


Well the totalShots value stays at 10 until you increase it, so totalShots % 10 == 0 will continue to be true until you increment totalShots the next time, so if you keep calling ShotCounter() it will increment the level.

You need to only either call it only once, or add some other check into it. I suppose the easiest fix would be to call ShotCounter() right after incrementing totalShots, so it's only executed when that value changes.

Do u have the function ShotCounter() inside the main loop? Between the 10th and 11th spawn this function is being called maybe 100 times doing 100 level++, try to avoid that.

If u want to call the method inside the main loop u can change the function by that:

function ShotCounter() { level=totalshots/10;

}

11/10 is 1,1 then is assigned in an integer(level) being the new assignanent 1 without the .1

I am not sure where are you calling your ShotCounter() method, so I can give you a generalized solution on this.

You'll definitely need a second condition before you choose to increment the level count. One way would be to have a boolean field which'll be true as soon as you increase the totalShots variable. Check it as another condition in your if statement and if the level increases then make it false, so that level doesn't get increased before next increment of totalshots.

Code snippet 1:

var totalShots : int = 0;
var level : int = 0;
var isIncrementAllowed : bool = false;

function Start () {

}

function Update () {
transform.Translate(Vector3(0, direction * speed * Time.deltaTime, 0));
if (Time.time > lastSpawn + nextSpawn){
Instantiate(shot, new Vector3(transform.position.x - 1, transform.position.y, transform.position.z), Quaternion.identity);
totalShots++;
isIncrementAllowed = true;

lastSpawn = Time.time;
nextSpawn = Random.Range(2, 3);
}

}

function ShotCounter() {
if(totalShots % 10 == 0 && isIncrementAllowed){
level++;
isIncrementAllowed = false;
}
return(level);
}


Another way would be to save LastTotalShots in a variable and check if it's the same count or not. It'll remove any possibility of incrementing a level at similar totalShots count twice.

Code snippet 2:

var totalShots : int = 0;
var level : int = 0;
var lastTotalShots : int = 0;

function Start () {

}

function Update () {
transform.Translate(Vector3(0, direction * speed * Time.deltaTime, 0));
if (Time.time > lastSpawn + nextSpawn){
Instantiate(shot, new Vector3(transform.position.x - 1, transform.position.y, transform.position.z), Quaternion.identity);
totalShots++;

lastSpawn = Time.time;
nextSpawn = Random.Range(2, 3);
}

}

function ShotCounter() {
if(totalShots % 10 == 0 && lastTotalShots != totalShots){
level++;
lastTotalShots = totalShots;
}
return(level);
}