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!!!

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);
        }
    */
}

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.

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...

@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 script for the GUI where do you put it :S, very new to unity so :confused: still working things out lol