- Home /
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. 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;
}
}
}
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;
}
}
}
}
@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?
You mean this?
int currentLevel;
int score; // 1 or 2 or 3
if (PlayerPrefs.GetInt("Level_" + currentLevel) < score)
{
PlayerPrefs.SetInt("Level_" + currentLevel, score);
}
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.
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;
}
}
Your answer
Follow this Question
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