Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Necrohunter · May 21, 2018 at 09:31 PM · mobileplayerprefs

player prefs

hehoo all,

i have just 1 simple question, i couldnt find any answer for..

when i try to use my playerprefs like this:

     public int gold;
 
 void Start () {

     PlayerPrefs.SetInt("Gold", 25);
     print(gold);

     gold = PlayerPrefs.GetInt("Gold");

     PlayerPrefs.SetInt("Gold", gold*2);
     print(gold);
     }

when i start it, it prints me 0 and 25. shouldnt it print 25 and 50?? and how it comes it shows first 0 everytime i start the game? i set it to 25 with the last time?

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 Necrohunter · May 21, 2018 at 09:46 PM 0
Share

this one have the same issue:

     gold = PlayerPrefs.GetInt("Gold");
     print(gold);

     PlayerPrefs.SetInt("Gold", gold*2);
     print(gold);

printing 2 times the same value

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Necrohunter · May 21, 2018 at 10:11 PM

okay.. i have another question to this one:

how can i say in 1 script, that i have in the playerprefs 1234 Gold units and from another script add +100 gold?

its just not working, i dont get it..

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
avatar image
0

Answer by Bunny83 · May 21, 2018 at 10:19 PM

Your problem is that the value stored in the playerprefs has no connection to your variable "gold". When you write a value to the playerprefs it won't affect your gold variable. In this snippet you actually set the playerpref value to two times the value stored in gold, but this won't change the value of your gold variable:

 PlayerPrefs.SetInt("Gold", gold*2);
 print(gold);


If you want to use playerprefs to store the latest gold value you should:

  • actually change your gold variable

  • Call SetInt after each change to the gold variable and simply store the current gold in the playerprefs

  • In Start you would re-initialize the gold variable at the beginning with the stored value by using GetInt


Note that GetInt has an optional second parameter which represents the default value if there hasn't been any value stored yet in the playerprefs. If you don't specify a default value, the value 0 is used.


I'm not sure what else we could say here as it's not clear what you want to do.

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 Necrohunter · May 21, 2018 at 10:45 PM 0
Share

well, i am creating a simple mobile game where i have a gold value. everytime i make a match, i want to add an amount of gold. an the biggest problem is, i dont know how to add this amount to my gold units.

i tryed to say in other scripts: PlayerPrefs.SetInt("Gold", gold + 100); or PlayerPrefs.SetInt("Gold", + 100);

but im really not sure what i do wrong.. how can i access from another sripts to the main script where i coded the palyerprefs?

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

122 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 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 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 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 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 avatar image avatar image

Related Questions

PlayerPrefs Storage Location on Android or iOS 4 Answers

Datasource for C# Android game 0 Answers

Read PlayerPrefs in Start()? 1 Answer

Where to save player progress? 0 Answers

Keeping Highest score - Star Rating System 3 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