Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 TheChanger · Jul 22, 2017 at 02:44 PM · uisceneplayerprefs

Unlockable Levels

I'm sorry if I may seem really angry but I have every right to be. For the past 4 months I have been working on this one thing unlockable levels. Any video I watch doesn't help at all and if anything just makes it worse by making me more angry that no-one can find a solution for me. I know what player preferences are but to me they make no sense at all. What I want to do is once I have completed a level it sends you to the main menu where you can select the next level or any levels before that. Sadly all things I have tried have not worked at all so I'm looking at unity answers for help (again). So if anyone replies could they please say exactly what I have to do as a beginner and I will be the happiest person ever. Btw I have multiple levels so I need the gamer to keep on progressing after every level by unlocking the next. So to stop this question from being vague the questions are: How do I make unlockable levels? How do you use player preferences and how do they work? Thanks in advance for any replies. Btw I cannot respond to you for a week as I am going on holiday soon so just assume things or wait for a while. Thx.

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
2

Answer by ShadyProductions · Jul 22, 2017 at 03:37 PM

Hello, I have written a script for you to use.

It will load your scenes by name.

Here are all the methods you can use and how to call them:

In order for you to call any method, (you can do this from any script and any scene) use the following:

 LevelHandler.Instance.METHODNAME

Here are a list of all the methods:

 LoadLevel(levelname); //will load a new level
 LockLevel(levelname); //will relock the level
 UnlockLevel(levelname); //will unlock the level
 IsLevelUnlocked(levelname); //will return true/false if level is unlocked or not

Here is the main script (this cannot be put on a gameobject and should not be attempted to).

 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class LevelHandler
 {
     private List<string> _unlockedLevels;
     private List<string> unlockedLevels
     {
         get
         {
             if (_unlockedLevels != null) return _unlockedLevels;
             _unlockedLevels = new List<string>();
             return _unlockedLevels;
         }
     }
 
     private static LevelHandler _instance;
     public static LevelHandler Instance
     {
         get
         {
             if (_instance != null) return _instance;
             _instance = new LevelHandler();
             return _instance;
         }
     }
 
     public LevelHandler()
     {
         // Load the save data
         LoadLevelData();
     }
 
     private void LoadLevelData()
     {
         // Load our unlockedLevel data
         string data = PlayerPrefs.GetString("unlockedLevels");
 
         // Add them to the list
         _unlockedLevels = data.Split(',').ToList();
     }
 
     public bool IsLevelUnlocked(string levelName)
     {
         return unlockedLevels.Any(f => f == levelName);
     }
 
     public void UnlockLevel(string levelName)
     {
         // Add to the current loaded data
         if (!unlockedLevels.Any(f => f == levelName))
         {
             unlockedLevels.Add(levelName);
 
             // Save player pref with unlocked levels comma seperated
             PlayerPrefs.SetString("unlockedLevels", string.Join(",", unlockedLevels.ToArray()));
         }
     }
 
     public void LockLevel(string levelName)
     {
         // Remove from the current loaded data
         if (unlockedLevels.Any(f => f == levelName))
         {
             unlockedLevels.Remove(levelName);
 
             // Save player pref with unlocked levels comma seperated
             PlayerPrefs.SetString("unlockedLevels", string.Join(",", unlockedLevels.Select(f => f.ToString()).ToArray()));
         }
     }
 
     public void LoadLevel(string levelName)
     {
         // Load the scene
         SceneManager.LoadScene(levelName);
     }
 }
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 Vollmondum · Jul 22, 2017 at 07:04 PM 0
Share

There's just a few simple lines he needs for Unlockability

 var maxUnlock: int;
 var levelToLoad: int;
 
 Start()
 {
 maxUnlock = PlayerPrefs.GetInt("$$anonymous$$axUnlock"); // which is set to PlayerPrefs.SetInt("$$anonymous$$axUnlock", PlayerPrefs.GetInt("$$anonymous$$axUnlock") + 1) upon level completion.
 }
 
 Update()
 {
 if(levelToLoad <= maxUnlock)
 {
 Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene(levelToLoad);
 }
 else
 {
 print("Sorry dude, no can do");
 }
 }

Why complicate 10 lines with 80? :) Another thing if he has side levels as a bonus, but yeah, great script.

avatar image ShadyProductions Vollmondum · Jul 22, 2017 at 11:36 PM 0
Share

$$anonymous$$ine has more versatility, i'm sure it might come in handy. :P + If the player unlocks level 1, and 3 but not 2, then yeah you got a problem :P

avatar image
0

Answer by Vollmondum · Jul 22, 2017 at 03:33 PM

PlayerPrefs are of three kinds: String, int and float. How it works:

     PlayerPrefs.SetInt("yourInt", 1);
     PlayerPrefs.SetFloat("yourFloat", 1.15);
     PlayerPrefs.SetString("yourString", "String is set up!");

Declaring that stores information about stuff you need. Say, upon completion a level, you Set an integer to the next level index. So before loading a level, you check that PlayerPrefs.GetInt("yourInt"); And load levels depending on response.

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 karthikbh · Jul 22, 2017 at 04:57 PM

I had same problem during my first game. This guy gave awesome explanation for that https://www.youtube.com/watch?v=rh3z8IqY_G0

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

119 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

Related Questions

How to Load PlayerPrefs from a different scene 1 Answer

Slider won't slide, issue assigning PlayerPrefs and then changing the PlayerPrefs' value 1 Answer

Text MeshPro Input field insert integer? 1 Answer

Change 2D background image across all scenes. 1 Answer

Get Text from an Input Field on one scene and use it as a text on another scene Unity c# 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