- Home /
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.
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);
}
}
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.
$$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
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.
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
Your answer
Follow this Question
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