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
1
Question by rainyy · Mar 17, 2011 at 10:49 AM · playerprefs

How to use PlayerPrefs?

Hi guys.

How can i use PlayerPrefs to save my game? Is there any tutorial available out there (JavaScript) ? If not, can someone give me an example on how to use it?

Here is a piece of my code:

Unlocking level code:

static var level2unlock : boolean;
var level2 : UIButton;
function Update () {
if(!level2unlock)
{
level2.controlIsEnabled = false;
}
else
{
level2.controlIsEnabled = true;
}
}

Code used in level 1. If a condition is met, unlock level 2.

if (transform.position.y > 47)
{
levelunlock.level2unlock = true;
}

I know that i must use PlayerPrefsX to save this kind of information, but i haven't used PlayerPrefs before so i still have no idea. Anyone can help me?

Thanks

Comment
Add comment
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

2 Replies

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

Answer by Skjalg · Mar 17, 2011 at 11:32 AM

Since a boolean value is just 0 or 1, then you can use:

SetInt Sets the value of the preference identified by key.
GetInt Returns the value corresponding to key in the preference file if it exists.

So when you do:

static var level2unlock : int = PlayerPrefs.GetInt("Level2Unlock", 0);

then in your function you do

if(level2unlock == 1)
{
  level2.controlIsEnabled = true;
}
else
{
  level2.controlIsEnabled = false;
}
PlayerPrefs.SetInt("Level2Unlock", (level2.controlIsEnabled ? 1 : 0));

PS: Code needs to be adapted, since I normally program in c# and this was done in the answer box here :)

Comment
Add comment · Show 2 · 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 rainyy · Mar 17, 2011 at 11:50 AM 0
Share

Hey.. Thanks for your answer :D I edited the code like the above, however, when i hit play, the level 2 is unlocked already :-/ What should i change? :)

avatar image Skjalg · Mar 20, 2011 at 12:34 PM 0
Share

The thing with PlayerPrefs is that it stores the values in the registry so that the next time you open your game it will also be unlocked.

So basically, once you've unlocked something it will stay unlocked forever. One way around this problem is to have the player type in a name for himself, then when you store something you do PlayerName + "Level2Unlock" in the GetInt method..

avatar image
0

Answer by O-R-C · Feb 19, 2014 at 01:47 AM

You could write a script that say "when get button reset PlayerPrefs" for instance.

 if(Input.GetKey(KeyCode.B))
     {
     PlayerPrefs.SetInt("Level2Unlock", 0);
     }

this will reset you playerprefs I think. You will have to remove this code when you finnish your game though.

Comment
Add comment · 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

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

1 Person is following this question.

avatar image

Related Questions

Check the size of PlayerPrefs from web player 2 Answers

PlayerPrefs save after quit? 2 Answers

loop through playerprefs ints and find specific one 2 Answers

Audio not playing on TriggerEnter (Pic Included) 3 Answers

UI Slider, Save Values and Change Them 0 Answers


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