- Home /
PlayerPrefs to make unlockable levels
Still working on my first game, it's for iPhone/iPad. Currently I'm gathering information about PlayerPrefs, using other questions on forum. What I want is to have my levels unlocked when Player collides with LevelSwitch and there is next level loaded. I suppose that LevelSwitch should have a script attached which checks for collision with Player, then according to number of current level sets next level to unlocked (true) and saves it to PlayerPrefs, and shows some menu (GUISkin) with buttons to go to MainMenu or play next Level. Then in my MainMenu I need a list of icons(buttons) which show state of each Level, for example, locked icons are in grayscale.
var level1 : int = 0; var level2 : int = 0; //and so on...
//this would be in level1 function OnTriggerEnter (other : Collider) { if (other.gameObject.tag == "Player" ) { PlayerPrefsX.SetInt("level2", 1); //set menu with two buttons active } }
In main menu with buttons to select level Script should check, which levels are unlocked and which buttons need to be set active:
var level1Unlocked = PlayerPrefsX.GetInt("level1"); var level2Unlocked = PlayerPrefsX.GetInt("level2"); //and so on...
function OnGUI () {
if(level2Unlocked == 1){ //draw button and all other stuff }
Am I actually going in the right direction? I'm taking for example this thread:
http://answers.unity3d.com/questions/16591/playerprefs-problem-crashing-with-setbool
I also run in the next problem :
http://answers.unity3d.com/questions/51984/iphone-resume-after-pressing-home-button
I hope some of you have answers. Thanks!!!
If you want to make a level lock unlock system, then here is a link to an article. It helped me a lot.
http://www.thegamecontriver.com/2014/09/create-level-lock-unlock-system-unity-46.html
Answer by GameGuy · Apr 12, 2011 at 12:43 PM
You wrote "PlayerTag" but if you have not renamed it yourself to this it should be "Player" only. You can also check what PlayerPrefs has written to the Registry, sometimes there is orphaned Data you need to delete. I`am using a simple button that deletes all PlayerPrefs data in the Registry.
http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.DeleteAll.html
Edit
I have looked over the scripts and reworked them, I hope it will work for you now.
Attach this to your Box:
//
function OnTriggerEnter(other : Collider)
{
//
if(other.gameObject.tag =="PlayerTag")
{
PlayerPrefs.SetInt("SavedLevel", 1);
Debug.Log("SavedLevel = 1");
//
Application.LoadLevel("menu");
}
}
...and this is the GUICode :
//
var level1Unlocked : Texture2D;
var level1Locked : Texture2D;
//var level2Unlocked : Texture2D;
//
var levelReached : int = 0;
levelReached = PlayerPrefs.GetInt("SavedLevel");
//
function OnGUI ()
{
//
if(levelReached == 1)
{
GUI.Label (new Rect (500,300,50,40),level1Unlocked);
}
//
else if(levelReached == 0)
{
GUI.Label (new Rect (500,300,50,40),level1Locked);
}
//....same for level2
/*
if(levelReached == 2)
{
GUI.Label (new Rect (500,400,50,40),level2Unlocked);
}
*/
}
Well,I have actually renamed my tag, I messed up somewhere and had to rename it...
O$$anonymous$$, thanks, I will try this, hope it works...
GameGuy I don't know how else to say I appreciate your answer on this more than I can express. I've trying to wrap my head around playprefs for about 2 weeks now and was frustrated. After some time searching google let me to them post. Thank you so much. Also $$anonymous$$agone thanks for posting the question which lead to this answer
Answer by kennypu · Apr 11, 2011 at 11:53 PM
looks like you're doing it right, except its PlayerPrefs, not PlayerPrefsX.
PlayerPrefsX just have the extra functions for vectors, arrays, etc. The basic methods arn't included in there.
Answer by Valdemars Magone · Apr 12, 2011 at 08:42 AM
I'm testing this method now and it's not working...
I have a box with trigger and script:
var level2 : int = 0;
function OnTriggerEnter(other : Collider){ if(other.gameObject.tag =="PlayerTag"){
level2 = 1; PlayerPrefs.SetInt("theLevel2", 1); Application.LoadLevel("menu");
} }
This should set level2 to 1 - unlocked, and brings me to menu. In menu I have a simple OnGUI rectangle with 2 different textures, if level2 is set to 0, there is red texture, if level2 is set to 1, there is green texture.
var level2Unlocked = PlayerPrefs.GetInt("theLevel2"); var yes : Texture2D; var no : Texture2D;
function OnGUI() { if(level2Unlocked == 1){ GUI.Label (new Rect (500,300,50,40),yes); } else if(level2Unlocked == 0){ GUI.Label (new Rect (500,300,50,40),no); }
}
Unfortunately there is always the red one...
Am I setting the PlayerPrefs right? I just need to get the idea to continue looking for best solutions...BTW, I tested this on iPad...
Answer by matis · Apr 27, 2011 at 06:52 PM
@GameGuy Hi, man I try your script and I have problem. When I use just check for level1 I dont have any problem but when I add lines for level2 my menu dont show 2textures when is level open. When I set up 2closed levels menu show that correct(two correct textures) but when I have just 1open or 2open levels menu show just the only one(just one open level without closed one). If you understand me can you help me? thx my script:
// var level1Unlocked : Texture2D; var level1Locked : Texture2D; var level2Unlocked : Texture2D; var level2Locked : Texture2D; // var levelReached : int = 0; levelReached = PlayerPrefs.GetInt("Level1"); // function OnGUI () { // if(levelReached == 1) { GUI.Label (new Rect (500,300,50,40),level1Unlocked); } // else if(levelReached == 0) { GUI.Label (new Rect (500,300,50,40),level1Locked); } //....same for level2
if(levelReached == 2)
{
GUI.Label (new Rect (600,300,50,40),level2Unlocked);
}
//
else if(levelReached == 0)
{
GUI.Label (new Rect (600,300,50,40),level2Locked);
}
}
the problem is the operator in the if statemants, check http://www.devx.com/projectcool/Article/19994 ! By the way this isnt a forum, it was only a lucky break that I saw your Answer. Next time better start a new Question here on Unity Answers.
Answer by Killbunny.uK · Apr 10, 2012 at 08:36 PM
The script for the GUI where do you put it :S, very new to unity so :/ still working things out lol
Old quesiton I know but you'd put it on an empty game object in your level selection screen. It could go on any object but I use empty ones :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613122143im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make PlayerPrefs delete if the player didn't touch game finish collider 1 Answer
Achievements with PlayerPrefs... 2 Answers
levels are locked again after playing a lower level 0 Answers
Accessing bundle identifier at runtime on iPhone 1 Answer
How to save text and display using Touch Screen Keyboard 1 Answer