Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Myster61 · Jul 30, 2020 at 11:30 AM · spriteimagelooplevel selectstars

At my 3 star rating system per level the sprite only changes for one image at a time why?

I have Images above my level buttons and put them in a list public List<Image> image;. I want to change the sprite of that according to the earned star of that level. If you finish a level under a specific time a variable is updated and I am getting that variable to set the image to the according sprite. alt text The left level button has the index of 0 so has the sprite and it loads Level(0) because of the index but of course in game it says like the picture 1. What happens is that if I finish level 1 under a specific time the sprite of the stars above the button changes accordingly. But if I play level 2 (current level index 1) and finish under a specific time the sprite above (stars) of level button that says "2" changes accordingly to the earned stars but the sprite of the previous button changes back.

Only 1 sprite at a time changes why?

  public void InitStar()
     {      int i = Manager.Instance.currentLevel;
         foreach (Image img in image)
         {
             if (PlayerPrefs.GetInt("Level_" + i) == 3)
             {
                 
                 image[i].sprite = starSprite3;               
             }
             if (PlayerPrefs.GetInt("Level_" + i) == 2)
             {
                 
                 image[i].sprite = starSprite2;
             }
             if (PlayerPrefs.GetInt("Level_" + i) == 1)
             {
                 
                 image[i].sprite = starSprite1;
             }
             if (PlayerPrefs.GetInt("Level_" + i) == 0)
             {
                
                 image[i].sprite = starSprite0;
             }
 
         }
     }

1.png (135.6 kB)
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by smark12007 · Jul 30, 2020 at 02:48 PM

Seems that you need to init image of ALL levels on start. Here is the modified script you could try:

     public void InitStar() {
         int i = Manager.Instance.currentLevel;
         for (int j = 0; j <= i; ++j) {
             foreach (Image img in image) {
                 if (PlayerPrefs.GetInt("Level_" + j) == 3) {
 
                     image[j].sprite = starSprite3;
                 }
                 if (PlayerPrefs.GetInt("Level_" + j) == 2) {
 
                     image[j].sprite = starSprite2;
                 }
                 if (PlayerPrefs.GetInt("Level_" + j) == 1) {
 
                     image[j].sprite = starSprite1;
                 }
                 if (PlayerPrefs.GetInt("Level_" + j) == 0) {
 
                     image[j].sprite = starSprite0;
                 }
             }
         }
     }
Comment
Add comment · Show 2 · 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
avatar image Myster61 · Jul 30, 2020 at 04:58 PM 0
Share

@smark12007 Thank you so much. One last thing if you could help me with it is if the playerpref = 3 and the player later finishes the game at a slower time. How to make it so that if the given star lets say is 3, it can never decrease to 2 or 1 anymore. how would this work?

avatar image smark12007 Myster61 · Jul 31, 2020 at 08:09 AM 0
Share

You mean this?

 int currentLevel;
 int score; // 1 or 2 or 3
 if (PlayerPrefs.GetInt("Level_" + currentLevel) < score)
 {
     PlayerPrefs.SetInt("Level_" + currentLevel, score);
 }
avatar image
0

Answer by Myster61 · Jul 30, 2020 at 05:04 PM

  private void OnTriggerEnter(Collider other)
     {
         int currentIndex = Manager.Instance.currentLevel;
         if (other.tag == "Player")
         {
             leveltime = 0;
             
  
             Debug.Log("victory");
            
             float duration = Time.time - startTime;
             if (duration < goldTime)
             {
                 leveltime +=3;
                 SaveManager.Instance.state.gold += 10;
                 PlayerPrefs.SetInt("Level_" + currentIndex, leveltime);
 
             }

this is how I save the int value in playerprefs.

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

Answer by Lozoo501 · Jul 30, 2020 at 06:27 PM

There are many issues with your code which you can figure out yourself XD

Long story short this is what would suffice but I bet that it could even be better, however I have already spent too much time on this and honestly this solution of mine is way better than the old one and even better than the code you got from smark12007. Because he didn't take into account many things that are wrong with the original code in the first place.

⠀

I have used professional naming conventions and code structure in my solution, which you can start using if you want or change into whatever you want.

⠀

 public void InitializeStarImages()
 {
     for (byte i = 0; i <= LevelManager.Instance.MaxLevel - 1; ++i)
         switch (PlayerPrefs.GetInt("Level_" + i))
         {
             case 0:
                 {
                     starImages[i].sprite = starSprite0;
                 }
 
                 break;
             case 1:
                 {
                     starImages[i].sprite = starSprite1;
                 }
 
                 break;
             case 2:
                 {
                     starImages[i].sprite = starSprite2;
                 }
 
                 break;
             case 3:
                 {
                     starImages[i].sprite = starSprite3;
                 }
 
                 break;
         }
 }
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.

Follow this Question

Answers Answers and Comments

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

Cast object as sprite for UI Image 1 Answer

Images information 0 Answers

Rare players have serious issues with disappearing or distorted images and sprites 0 Answers

Problem with Image and Sprite 1 Answer

Adding Images for Quiz Game 0 Answers


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