• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by munggol97 · Oct 23, 2018 at 12:18 PM · poweruppickupscollectiblestarspick-up

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();
     }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Casiell · Oct 23, 2018 at 12:38 PM 0
Share

But what doesn't work? The code should do what you described, what is wrong?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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();


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

94 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Outer Space Visuals 5 Answers

How to make PlayerPrefs delete if the player didn't touch game finish collider 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges