- Home /
Updating main menu using PlayerPrefs when collecting an object in different scene (level)
I've read the docs on PlayerPrefs but do not fully understand how they work. I've found several posts on Unity Answers regarding similar issues but am not having much luck. So here's the situation: When the player collects an object in a level I want this to be reflected when the player returns to the main menu screen. Or if the player beats a level I want to be able to select the next level in the main menu screen. Here is how I was going about it:
On my first script, in my level scene when I collected the collectible I put this as the last line of the script:
PlayerPrefs.SetInt("Level2Unlocked", 1);
On my second script, in my main menu scene I put this onto an empty game object :
using UnityEngine;
using System.Collections;
public class MenuButtonLock : MonoBehaviour {
public GameObject[] buttonLocks;
public GameObject[] buttonText;
void Start () {
if (PlayerPrefs.HasKey("Level2Unlocked")) {
NGUITools.SetActive(buttonLocks[0], false);
NGUITools.SetActive(buttonText[0], true);
}
}
}
Should I be putting this in it's own function instead of Start? I am trying to understand how this all works. Hopefully someone can shed some light on PlayerPrefs.
Answer by stingman · May 21, 2012 at 02:55 AM
answered my own question... my code above works perfectly with one change... just needed to put the code in the update function... not the start function. always something small but regardless after spending 3 straight hours researching PlayerPrefs I feel I understand them a lot better :)