• 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
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

People who like this

0 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
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

People who like this

0 Show 0 · 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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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? 3 Answers

Outer Space Visuals 5 Answers

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges