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 eeveelution8 · Jun 22, 2014 at 04:55 AM · playerprefsjava

Question on playerprefs

i have been working with different functions and values in coding, as i'm still learning. And im considering the prospects of using the specific functions with playerprefs like setint and getint. I understand how you. can set the value of a key, and retrieve it's value, this is what i understand, but what I'm confused about is how the value is created in the first place.

If i were to set an int value on startup, and require the int value to be a certain number to unlock a character for a game for example, and have it initially set to 0. Upon completing an arbitrary condition, the int value is set to 1, and the character will be unlocked. But if I quit the game, Would it be reset on startup because of the setint function?

The jist of my question is how to create the int value once with a value of a number in the first time the game is run and not have it reset upon loading the game up again.

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 Nick4 · Jun 22, 2014 at 06:09 AM

PlayerPrefs holds the data in a simple file so it won't be deleted when you quit your game. However you need to load the value (using GetInt) when you start your game and save it (using SetInt) when you quit your application.

I see you're worried about setting int to 0 everytime you start your game. You can just use PlayerPrefs.HasKey function in order to determine whether you alread stored a variable with a particular key or not. If there's no data paired with your key, you can set your variable to 0.

 void Start()
 {
     if(PlayerPrefs.HasKey("charactersPoint") == false)
     {
         // There's no data named "charactersPoint" yet.
         // It seems like it's the first run of your game, 
         // feel free to create one
         PlayerPrefs.SetInt("charactersPoint", 0);
     }
 }

Edit : You don't want to store a sensitive data (such as yours in this case) with PlayerPrefs, anyone can edit it. As the name implies, it's the player preferences. You'll want to serialize your data for important data. Mike did an awesome tutorial about this subject. Good luck.

Comment
Add comment · Show 1 · 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 zackmah · Jul 26, 2016 at 10:48 AM 0
Share

Hi @Nick4 I have the same problem and your answer solved it however I got one more issue. I need to restart my game once my life is finish. so when I press the restart buttion then two life should be added. But right now, when I restart my game I get 0 life. How can I reassign two value?

avatar image
1

Answer by Kiwasi · Jun 22, 2014 at 06:00 AM

Use GetInt first. If the int does not exist it will return 0, or some other default value of your choice. This allows you to check if anything is in PlayerPrefs before you use SetInt

Edit: Just scanning the documentation now, you could also use HasKey

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

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

player pref first time set up 2 Answers

Tags Help scripting 1 Answer

How to add the values of all playerpref ints with 1 Answer

Data keeps "hanging". TCP client/server 2 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