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 /
  • Help Room /
avatar image
0
Question by Etzix · Nov 23, 2015 at 07:02 PM · scripting problemplayerprefssavesaveload

Saving item count with Playerprefs

So i'm new to programming and writing scripts, and ive decided to make an incremental game just for fun and to learn new things. and i have come to a point where i think i should implement some kind of save system, before i add a bunch more stuff.

So i started using PlayerPrefs , and it works great for saving gold and other variables, but im having problems with saving how many upgrades i have bought. All my upgrades are in one script, and if i save Count , it thinks that every upgrade should have the same count.

Heres a pastebin http://pastebin.com/JspjPdDa , ignore the Playerprefs, i was just trying out different strategies. Thanks in advance.

Comment
Add comment · Show 4
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 fredz0004 · Nov 24, 2015 at 03:22 AM 0
Share

Getting a bit confused with this part:

All my upgrades are in one script, and if i save Count , it thinks that every upgrade should have the same count.

So you have different upgrades, which should have different counts?

avatar image Etzix fredz0004 · Nov 24, 2015 at 08:22 AM 0
Share

Yes that's right, with my script im able to add multiple upgrades that all use the same script and have different counts, but I'm not able to save them this way.

avatar image fredz0004 Etzix · Nov 24, 2015 at 11:24 AM 0
Share

huh I had answered yesterday. Did it get deleted?

Show more comments

1 Reply

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

Answer by fredz0004 · Nov 24, 2015 at 02:10 PM

No worries not sure what happened I submitted an answer yesterday. Anyways if you need different count values for different upgrades. You need to be able to differentiate your upgrades. Give your upgrades different ID values in PlayerPrefs, and associate that ID to a count value in PlayerPrefs.

Small example:

 string upgradeID = 3;
 int upgradeCount = 43;
 PlayerPrefs.SetInt(upgradeID, upgradeCount);

This should guide you in the right path, you just need to make sure each time you generate a new upgrade, you give it a new ID somewhere in your script. When you are testing the logic, just print in a forloop all the available upgrades, and see if they have different count values.

Something like this:

     string upgradeID = 3;
     int upgradeCount = 43;
     PlayerPrefs.SetInt(upgradeID, upgradeCount);
     
     for (int i = 0; i < numberOfUpgrades; i++) {
     
     print ("This is upgrade ID" + i + "its count value is " + 
     PlayerPrefs.GetInt(i.ToString()));
     }

Let me know if it works.

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 Etzix · Nov 24, 2015 at 02:41 PM 0
Share

I'm still confused , I'm able to have different count values already on different upgrades , But i can't save it because they all use the same Int i suppose. I realise now that im going to need different ID's thanks to you but im still not sure how to implement it.

As you have most likely noticed im using Unity's UI feature , so when i ''buy'' an upgrade it only updates the count for that specific button/upgrade. But i have no way to store that count.

I'm sorry if im co$$anonymous$$g off as really stupid , this is the first time im making anything at home and not in School. I still have a lot to learn so i appreciate all the help.

EDIT: I got it working , Thanks a lot! I think i even solved the problem with updating the prices so you cant buy it for the base price when you first start the game , altough to do that i have to update the price in Void Update all the time , so if there's any better way to do it please share it with me!

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

37 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

Related Questions

Problem with saving player data 0 Answers

PlayerPrefs Autosave [SOLVED] 2 Answers

The left-hand side of an assignment must be a variable, a property or an indexer 1 Answer

save one big json string vs multi seperate json strings in playerprefs 0 Answers

PlayerPrefs mad?! 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