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 AliCollinsJSY · Mar 02, 2014 at 06:05 AM · c#playerprefssavedontdestroyonloadcurrency

Currency System Not Saving!?

Here's the situation and an example of the problem with my currency system (all in C#). I plan for the player to be able to purchase upgrades between levels if they have enough gems.

Level 1: Player collects 100 gems. (100 gems is saved in PlayerPrefs).

Level 2: Player collects 50 gems. (150 gems saved in PlayerPrefs). So currency DOES save between levels.

However, I have only created 2 levels so far and I have made the player go back to the splash screen at the end of level 2. In which case, when the player reloads Level 1, the gem count is saved and loaded (via PlayerPrefs) but the Gem objects in the level reload - so the player can collect infinite gems. I have set the Gems to be destroyed on touching the Player. Once the gem has been destroyed in-game, I want it to permanently be destroyed (unless the player resets their current save) so the player can only gain the maximum number of gems in any given level.

GEMS:

Here is the Gem script and the relevant excerpt from the Player script.

 void OnTriggerEnter(Collider c)
 {
     if(c.gameObject.tag == "Player")
     {
         Destroy(gameObject);
 }
 }

PLAYER:

 void Awake () {
                   Load ();
 }
     void Save ()
                   {
             PlayerPrefs.SetInt("Gems Currency",Gems);
                    }
 
 void Load () {
 
 Gems = PlayerPrefs.GetInt("Gems Currency");
 
 }

It saves at the end of every level. Hope I've made this question clear enough. If you need more information, please let me know and I can make a video to better explain the problem. Hope somebody can help, major sticking point here!

A.C.

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 robertbu · Mar 02, 2014 at 06:08 AM 0
Share

In order to prevent the user from not re-collecting gems if he returns to a level you either have to 1) save the number of gems acquired on each level and randomly place any remaining, or 2) save the state of each gem. If you lean towards the latter solution see the ArrayPrefs2 script in the Unity Wiki for a solution to saving the state of all your gems on each level.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by whydoidoit · Mar 02, 2014 at 06:23 AM

So the best way to do this is to create a unique ID for each gem in a script and have that be auto destroyed if it has already been collected by saving away the ids.

That's pretty easy to do like this, you would add this script to the gems:

OnlyUseOnce.cs

   using System.Collections;
   using UnityEngine;

   [ExecuteInEditMode]
   public class OnlyUseOnce : MonoBehaviour {

           public string uniqueId;

           void Awake() {
                 uniqueId = string.IsNullOrEmpty(uniqueId) ? System.Guid.NewGuid().ToString() : uniqueId;
                 if(Application.isPlaying && PlayerPrefs.GetInt("USED_" + uniqueId, 0) == 1) {
                     Destroy(gameObject);
                 }
           }

           public void UseMe() {
                  PlayerPrefs.SetInt("USED_" + uniqueId, 1);
                  Destroy(gameObject);
           }

   }

Now rather than Destroying the gem when the player picks it up you use:

       SendMessage("UseMe");

Or

       gemObject.GetComponent<OnlyUseOnce>().UseMe();
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 AliCollinsJSY · Mar 02, 2014 at 06:26 PM 0
Share

I like the unique ID system, however it seems Unity cannot save/load booleans as Player Prefs:

12: Error CS0117: 'UnityEngine.PlayerPrefs' does not contain a definition for 'GetBool' (CS0117) (Assembly-CSharp)

18: Error CS0117: 'UnityEngine.PlayerPrefs' does not contain a definition for 'SetBool' (CS0117) (Assembly-CSharp)

I've also tried replacing "GetBool" with "Getbool" and "GetBoolean" just to check it wasn't a spelling error or syntax error, but still no luck. Any idea how to fix this so I can test the unique IDs for the gems?

Ps Thank you for your help thus far!

avatar image whydoidoit · Mar 03, 2014 at 08:55 AM 0
Share

I'm an idiot - updated

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

21 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

Related Questions

Save values on different objets with same script? 1 Answer

Drag and Drop PlayerPrefs 2 Answers

How to stop objects you picked up from reappearing when you go to another level and return 1 Answer

How to save different values with same script? 1 Answer

Problems with saving/loading score with PlayerPrefs [C#] 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