Hey all i have been trying to work out PlayerPrefs and i cannot figure it out.
Basically what im trying to do i, when the player plays the game 5 times they will unlock a new item.
Basically this is what im doing so far,
var PlayCount:int;
function Update(){
PlayerPrefs.SetInt (“Count”,PlayCount);
PlayerPrefs.GetInt(“Count”, PlayCount);
if (PlayCount ==5)
{
print (“hi”);
HalloweenUnlock=true;
}
}
Then im doing this to add 1 to the PlayCount
PlayCount +=1;
It is working and when it gets to 5 it unlocks the item however it doesn’t save.
So i was wondering how to save it so when you play the game later it is still unlocked.
The problem is that you reset all the playerprefs every time you run the game!
If you want it saved between games, do the getting bit before the setting bit.
So, do something more like this-
var PlayCount : int
var thingUnlocked = false;
function Update(){
var hasUnlocked = PlayerPrefs.GetInt("HasUnlocked");
if(hasUnlocked == 1)
{
thingUnlocked = true;
}
// Do your PlayCount incrementing however you want to (I'd use a coroutine and WaitForSeconds(60))
if(PlayCount >= 5)
{
thingUnlocked = true;
PlayerPrefs.SetInt("HasUnlocked", 1);
}
}
This way, you only store whether the thing has been unlocked yet instead of trying to store how long the player has been playing for! Of course, if you wanted it to be cumulative, you would store that as well, but as far as I can tell that’s not what you need here.