- Home /
PlayerPrefs for unlocking items
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.
Don't write "solved" in the title, use the accept button.
Answer by syclamoth · Oct 17, 2011 at 01:42 AM
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.
Hell yeah man that is sweet as.
Thanks for your quick reply, its so straight forward now its laid out in front of me haha.
Thanks again man.
$$anonymous$$ark me as the 'accepted' answer then- I do like green highlighting on my text!
your answer will never get accepted... $$anonymous$$UAHAHAHAHA! but I did upvote it for you just to be nice. :)
Your answer
Follow this Question
Related Questions
onapplicationquit() function is not working with unity 4.6.3 (android) 3 Answers
Saving Location in Unity 1 Answer
Load AssetBundles on Android device 1 Answer
How to save an array of string in Android? 1 Answer
PlayerPrefs not saving 1 Answer