Having trouble increasing level inside of Update() when score reaches 1000...
Problem is that I can't figure out how to make the program increment the level once when the score reaches 1000 but then stop and not increment it again until the user reaches 2000. Currently Update() calls it again because the user doesn't have enough time to score another point before all the levels have been incremented. How would I instrument the below code to increment the level once when user hits 1000 points but then not again right away like it's doing now?
// incrementing levels every 1000 points
if(PlayerPrefs.GetInt("CurrentScore") % 1000 == 0 && PlayerPrefs.GetInt("CurrentScore") != 0)
{
print(PlayerPrefs.GetInt("CurrentScore")); //for debugging
Invoke("incrementLevel", 0);
//incrementLevel();
}
Level Increment Function:
//function to increment level
private void incrementLevel()
{
if (level <= (int)levels)
{ level++; }
}
UPDATE, SOLVED:
I solved my own problem by sleeping and waking up with an idea. I'm sure this is how it was going to be suggested to me anyhow, I feel stupid for even asking the question now. Let me know if this is an inefficient way of doing it though.
First I created an int tempScore variable set to 0. (not shown)
Then I simply checked after the user hit 1000 points if currentScore is larger than tempScore before allowing the increment.
If it was larger, increment would happen then tempScore would be assigned to currentScore so that it doesn't go in again until the score is 2000.
// incrementing levels every time person scores 1000 points
if(PlayerPrefs.GetInt("CurrentScore") % 1000 == 0 && PlayerPrefs.GetInt("CurrentScore") != 0)
{
if (PlayerPrefs.GetInt("CurrentScore") > tempScore)
{
if (level < ((int)levels-1))
{
print(PlayerPrefs.GetInt("CurrentScore"));
incrementLevel();
tempScore = PlayerPrefs.GetInt("CurrentScore");
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612062157im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Spawn added objects into a circle 1 Answer
how to do on single click fire and on drag appears the joystick and get the input from joystick 0 Answers
Movement Unit (RTS) 0 Answers
Car from Unity's standard assets giving me of null reference errors 0 Answers
How do I resolve, get_isActiveAndEnabled can only be called from the main thread error 1 Answer