- Home /
How can I add different amount of stars depending on points reached?
Hello, so I am creating a game that rewards a star when a certain score is reached. If the player reached 15 points he gets 1 star and when the score increases to 50 points he gets another 2 stars and when he reached 70 points he gets another 3 (so 5 stars will be added to his current stars). And then the stars he collected will be saved. Please help me how can I implements that scenario I have tried some codes but doesn't work well.
Here is my current code:
if (Score == 15) {
         starAmount += 1;
         starC.text = starAmount.ToString();
     }
     if (Score == 50)
     {
         starAmount += 2;
         starC.text = starAmount.ToString();
     }
     if (Score == 70)
     {
         starAmount += 3;
         starC.text = starAmount.ToString();
     }
     if (starAmount > PlayerPrefs.GetFloat("stars", 0))
     {
         PlayerPrefs.SetFloat("stars", starAmount);
         starC.text = starAmount.ToString();
     }
But what doesn't work? The code should do what you described, what is wrong?
Answer by Hellium · Oct 23, 2018 at 12:51 PM
I believe you should use less or equal comparison with else if instead of equal comparisons.
 if (Score < 15 )
      starAmount = 0;
 else if (Score < 50)
      starAmount = 1;
  else if (Score < 70)
      starAmount = 2;
  else
      starAmount = 3;
  if (starAmount > PlayerPrefs.GetFloat("stars", 0))
      PlayerPrefs.SetFloat("stars", starAmount);
  starC.text = starAmount.ToString();
Using an array would be even better:
 int[] starsGainThresholds = new int[]{ 15, 50, 70 } ; // You can even put this as a public class member if you want to edit it in the editor
 for( int thresholdIndex = 0 ; thresholdIndex < starsGainThresholds.Length ; ++thresholdIndex )
 {
     if( Score >= starsGainThresholds[thresholdIndex] )
         starAmount++ ;
 }
  if (starAmount > PlayerPrefs.GetFloat("stars", 0))
  {
      PlayerPrefs.SetFloat("stars", starAmount);
  }
   starC.text = starAmount.ToString();
Your answer
 
 
             Follow this Question
Related Questions
Random Pickup Respawn Manager?(spawn pickups with the tag "PickUp" in random locations) 0 Answers
Increase the speed after collision 0 Answers
How to stop an item that has already been collected from respawning when player returns to a scene? 0 Answers
how to give star score once per each level? 0 Answers
Android Game crashing when collecting collectibles 0 Answers
