How can I increase the level Counter only once when player reached a certain amount of Score

Hello, I am new in unity and I would like to solve some problem.

There are 5 objects to be catched by player. Player will gain certain scores for catching each objects respectfully;
I would like to increase a counter when player encounters 1000 points every time.

To do this, I used mod operation like following;

        if ( Score % 1000 <= 50) //50 is the largest score to be gained when player catch an object


However, I would like to increase the levelCounter only once when score is between 1000 and 1050. for example; if score is 1000 but you catched obj1 again, therefore since score will be 1010, “if” operation above will work again. Can you help?

Hi @Kozankose,

while using the modulo operator works in theory, you could also divide your score by thousand and use the resulting int value as a level indicator.


levelcounter = (int)(Score / 1000);
if(levelCounter > 50)
    //handle what ever happens then

I would also recommend the solution that @Klarzahs posted. Though if you want to be able to increase the levelCounter independently from score (through other events, powerups, etc) you would essentially need an “edge detector” whenever you pass your desired score mark. For this you would need an additional state variable that you can use to create a proper delta value. You could just use an “oldScore” value that you set to the current Score at the end of Update.

This allows you to detect any rollovers at the moment it happened.

if (Score % 1000 < oldScore % 1000)
oldScore = Score;

However this has several issues. Score increases which are around the order of magnitude as your desired limit or a multiple of that limit would not be detected. So if you award 1000 or 5000 points at once this breaks down. A better solution is to remember the levelincrease per 1000 score points seperately

int levelBonus = Score / 1000;
inf levelIncrease = levelBonus - oldLevelBonus;

if (levelIncrease > 0)
    levelCounter += levelIncrease;

oldLevelBonus = levelBonus;

This way no matter how Score changes for everyl 1000 points you get one additional level. Also you can add other level increases seperately from this.