• 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 /
This question was closed Jul 22, 2018 at 05:45 PM by chiave15 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by chiave15 · Jul 22, 2018 at 04:17 AM · unity 5playerlevelcodepage

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 scriptalt text

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 alt text

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

bandicam-2018-07-21-22-28-44-308.jpg (73.5 kB)
bandicam-2018-07-21-22-40-50-442.jpg (42.4 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

2 Replies

  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 13 · 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 chiave15 · Jul 22, 2018 at 03:27 PM 0
Share

Ok..... I have one more tiny problem alt text

@UnityCoach

bandicam-2018-07-22-10-21-51-088.jpg (99.0 kB)
avatar image UnityCoach chiave15 · Jul 22, 2018 at 03:31 PM 1
Share

I don't see the attachement. What is the problem ?

avatar image chiave15 UnityCoach · Jul 22, 2018 at 03:34 PM 0
Share

I just reposted it...look again @UnityCoach

Show more comments
avatar image UnityCoach chiave15 · Jul 22, 2018 at 03:50 PM 1
Share

Ah yes, I believe you have to put it this way then :

 return (PlayerPrefs.GetInt("LevelsCompleted") & (1 << level)) != 0;
avatar image chiave15 UnityCoach · Jul 22, 2018 at 04:09 PM 0
Share

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

@UnityCoach alt text

bandicam-2018-07-22-11-08-33-848.jpg (70.2 kB)
Show more comments
avatar image
1

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.

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

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

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

I am getting player tag not the other object that i want to destroy. 1 Answer

Roll a Ball Tutorial: Ball floats in the air when I enter play mode. 2 Answers

How To Make a Character switch like in the Lego Games 1 Answer

It does not add money to player when it should ? 2 Answers

Zig Zag movement Unity 2 Answers

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