- Home /
The question is answered, right answer was accepted
How to let player know they have completed a level
I have a game where all levels are unlocked, I need to let the user know that they have completed a level, they click on a button to switch to a level my game has 15 levels so I have this script
The for loop is to check all the levels to see which levels have not been completed, if the level is not completed then the color is going to equal the color of a hidden button which is red
The thing is...the system I created works like a regular unlock and lock system (play level 1 to unlock 2, play level 2 to unlock 3 etc.)
so let's say I play level 7 (remember all levels are unlocked) it will leave the buttons after 7 (8 - 15) the level not played color which is red, which I want it to do, but the buttons before 7 (1 - 6) will be there normal color, indicating that the player has played levels 1 through 6 but they haven't. I need a fool proof way of informing a user that they have completed a level, even if they go to level 7 with out playing levels 1 - 6 it will only show hat they have played level 6 and not the levels before 6 as well.
P.S. the PlayerPrefs.DeleteKey("LevelIsNotCompleted");
is so before I export my APK I delete any saved data I might have stored during the development process in the unity editor. @UnityCoach
@AnyoneElseThatCanHelp
Answer by UnityCoach · Jul 22, 2018 at 02:09 PM
Hi,
If you have no more than 32 levels, you can store the completion as a bit array. In other words, a 32 bit integer of which every bit is used to store a boolean value.
bool GetLevelCompleted (int level)
{
return PlayerPrefs.GetInt("LevelsCompleted") & (1 << level) != 0;
}
void SetLevelCompleted (int level)
{
PlayerPrefs.SetInt("LevelsCompleted", PlayerPrefs.GetInt("LevelsCompleted") | (1 << level));
}
void Init ()
{
for (int i = 0 ; i < buttons.Length ; i++)
if (GetLevelCompleted(i))
buttons[i].colors = grapfic.colors;
}
Unless you really want to place the buttons manually, I would also use Layout components (Vertical, Horizontal or Grid), instantiate buttons through scripting, and assign listeners to buttons events automatically too.
Hope this helps.
Ah yes, I believe you have to put it this way then :
return (PlayerPrefs.GetInt("LevelsCompleted") & (1 << level)) != 0;
Ok...Smh… ok one more thing unity is doing that I need to fix, the code above is correct but unity is throwing this error in my face like a pound of crap lol
Answer by hexagonius · Jul 22, 2018 at 11:11 AM
Just do a playerprefs for each level. Extend your key name with the level number. Use Set and GetInt to set 0 or 1 depending on the state of the level, not played or finished. This way, each LevelButton gets it's own state, initialized with 0 as not played yet.
Follow this Question
Related Questions
cancle invokeReapeating not working 0 Answers
Player boundaries based on screen resolution 1 Answer
Help with Generating level 0 Answers
Guides or Tutorials for a Very Basic Enemy AI? [Unity 5] 1 Answer