- Home /
Show message when score achieved
I'm having a problem to call a function at certain scores, the problem is I'm adding points every second in update so I want to show a message at 10,000 then 20,000 and so on, the problem is when you kill an enemy you add 100 points also when you pick a power up you get 1000 points, so I can't use modulus (1000 % 0) to check on the score because this condition is not always met.
Then I used a condition like if(score > 10000){showMessage();} but the condition keeps creating the message until the next one (20,000) is met and so on, also tried with a bool to start and stop but either the bool keeps turning true/false or it doesn't get ready for the next condition...
I can't figure this one out :(
Answer by DCordoba · Feb 05, 2019 at 10:23 AM
you are at one step of the solution, just add a nested if to check if you sent the message
 //the bools
 bool sentM10K = false;
 bool sentM20K = false;
 
 //few lines later..., fit this on your code
 void CheckMessageSystem(){
 if(!sentM10K){
   if(score > 10000){
   showMessage();
   sentM10K = true;
   }
 }
 
 if(!sentM20K){
   if(score > 20000){
   showMessageTwo();
   sentM20K = true;
   }
 }
 //and so on
 }
you can change the !sentXK to a notSentXK just to increase performance and inverts the bools, but to make it readable I use this way
I see what you did there and it crossed my $$anonymous$$d at some point but didn't wanted to make that many bools for each level, I don't have so much levels here but could it be a way to make this solution work let's say for infinite levels?
Here's my final code:
              IEnumerator IncreaseDifficulty()
 {
     while (true)
     {
         if (!level2Reached)
         {
             if (scoreNumber > 10000)
             {
                 difficultySpawner = 7;
                 StartCoroutine(Pop$$anonymous$$essage("Nivel 2" + "\n" + great$$anonymous$$essages[Random.Range(0, great$$anonymous$$essages.Length)]));
                 level2Reached = true;
             }
         }
         if (!level3Reached)
         {
             if (scoreNumber > 20000)
             {
                 difficultySpawner = 6;
                 StartCoroutine(Pop$$anonymous$$essage("Nivel 3" + "\n" + great$$anonymous$$essages[Random.Range(0, great$$anonymous$$essages.Length)]));
                 level3Reached = true;
             }
         }
         if (!level4Reached)
         {
             if (scoreNumber > 30000)
             {
                 difficultySpawner = 6;
                 StartCoroutine(Pop$$anonymous$$essage("Nivel 4" + "\n" + great$$anonymous$$essages[Random.Range(0, great$$anonymous$$essages.Length)]));
                 level4Reached = true;
             }
         }
         if (!level5Reached)
         {
             if (scoreNumber > 40000)
             {
                 difficultySpawner = 5;
                 StartCoroutine(Pop$$anonymous$$essage("Nivel 5" + "\n" + great$$anonymous$$essages[Random.Range(0, great$$anonymous$$essages.Length)]));
                 level5Reached = true;
             }
         }
         if (!level6Reached)
         {
             if (scoreNumber > 50000)
             {
                 difficultySpawner = 5;
                 StartCoroutine(Pop$$anonymous$$essage("Nivel 6" + "\n" + great$$anonymous$$essages[Random.Range(0, great$$anonymous$$essages.Length)]));
                 level6Reached = true;
             }
         }
         yield return null;
     }
     
 }
I was solving this while you asking xd, check next comment
Edit: modificado tu codigo
 int level = 0;
 $$anonymous$$Difficulty = 7;
 IEnumerator IncreaseDifficulty()
  {
      while (true)
      {
         for(int i =level; i < (int)score/10000; i++){
            level++;//put this inside, or will increase one more...
            //seem to decrease level each two, so on unpairs levels
            StartCoroutine(Pop$$anonymous$$essage("Nivel " + level + "\n" + great$$anonymous$$essages[Random.Range(0, great$$anonymous$$essages.Length)]));
             difficultySpawner = $$anonymous$$Difficulty - (int)level/2;
         }
       }
 }
and, use a stop to execute once on each frame, like
 yield return new WaitForEndOfFrame ();
just after the for, inside the while ;)
if you have a infinite messages, with the same "distance"* (or to can be estimated distance like exponential?) between each, you can do some most wise to handle this
lets do a linear distance (equal between each message) of 10000 points like you proposed first
 int progress$$anonymous$$essage = 0;
 
 void Check$$anonymous$$essageSystem(){
     for(int i =progress$$anonymous$$essage; i < (int)score/10000; i++, progress$$anonymous$$essage++)
         show$$anonymous$$essage();//universal message, to personalize it use some like show$$anonymous$$essage(i*10000)
  }
and the personalized message is some like:
 void show$$anonymous$$essage(int Data){
     Text _text = GetComponent<Text>();
     _text.text = "you reached the " + Data + " points";
 }
- use word distance to say "the points from the last message to the next" 
Awesome, so just put the Check$$anonymous$$essageSystem(); at update and should work right? I mean problem was solved but I assume this is a most common problem and your answer I'm sure will help many people, thank you very much for your time :D
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                