Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by UpFrontGames · Oct 17, 2011 at 01:32 AM · androidplayerprefsloadsaving

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Oct 17, 2011 at 03:22 AM 1
Share

Don't write "solved" in the title, use the accept button.

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UpFrontGames · Oct 17, 2011 at 01:51 AM 0
Share

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.

avatar image syclamoth · Oct 17, 2011 at 02:02 AM 2
Share

$$anonymous$$ark me as the 'accepted' answer then- I do like green highlighting on my text!

avatar image Jibidiah · Oct 17, 2011 at 02:48 AM 0
Share

your answer will never get accepted... $$anonymous$$UAHAHAHAHA! but I did upvote it for you just to be nice. :)

avatar image syclamoth · Oct 17, 2011 at 02:55 AM 1
Share

Thanks! I appreciate upvotes.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges