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 kazzrberry · May 23, 2014 at 06:55 PM · playerprefs

Playerprefs resetting after quit

Hey! I'm having a problem with my code. My game is currently using 2 scripts, a GameManager (attaches to camera) and a SunScript (attached to a prefab (Sun) that is instantiated every 1 second onto the screen). When you click on the Sun, it destroys the gameObject and adds 10 points to your score, which I save using playerprefs, and then displays it using OnGUI.

The problem: when I quit the application and re-open, it displays the correct amount, but when I click on a Sun, it resets the counter. PLEASE HELP!

Here is the GameManager script:

 using UnityEngine;
 using System.Collections;
 
 public class GameManager : MonoBehaviour {
     public GameObject sunPrefab;
     private float rateDrop = 2f;
     public static float totalCurrency;
     public static float collectAmount = 152f;
     public GUISkin skin;
 
     void Start(){
         GenerateSun();
         Debug.Log(PlayerPrefs.GetFloat("Total Currency"));
     }
 
     void InstantiateSun(){
         Instantiate (sunPrefab, new Vector2(Random.Range(-6.0f, 0f), Random.Range(10.0f, 12.0f)), Quaternion.identity);
     }
 
     void GenerateSun(){
         InvokeRepeating ("InstantiateSun", 1f, rateDrop);
     }
 
     void OnGUI (){
         GUI.skin = skin;
         GUI.Label(new Rect(Screen.width/2 - 40, 10.0f, 40, 20), PlayerPrefs.GetFloat("Total Currency").ToString(), skin.GetStyle("Counter"));
     }
 
     void OnApplicationQuit(){
         PlayerPrefs.SetFloat("Total Currency", PlayerPrefs.GetFloat("Total Currency"));
     }
 }


And here is the SunScript:

using UnityEngine; using System.Collections;

public class SunScript : MonoBehaviour { public GameObject particles;

 void Update(){
     if(transform.position.y < -15.0f){
         Destroy(gameObject);
     }
 }

 void OnTriggerEnter(Collider other){
     if(other.transform.tag == "House"){
         Debug.Log("I hit the house!");
         Destroy(gameObject);
     }
 }

 void OnMouseDown(){
     Collect();
 }

 void Collect(){
     Instantiate (particles, transform.position, Quaternion.Euler(270,0,0));
     Debug.Log(PlayerPrefs.GetFloat("Total Currency"));
     GameManager.totalCurrency += GameManager.collectAmount;
     PlayerPrefs.SetFloat("Total Currency", GameManager.totalCurrency);
     Destroy(gameObject);    
 }

}

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
0

Answer by rutter · May 23, 2014 at 07:03 PM

You need to call PlayerPrefs.Save to save your changes.

Depending on your game, you might want to call this between levels (or during some other "pause" in the action)... or, if you don't set prefs very often, you might be able to get away with saving after each and every change.

Comment
Add comment · Show 3 · 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 kazzrberry · May 23, 2014 at 08:13 PM 0
Share

The game doesn't really have any "levels", it is a single scene where blocks fall from the top of the screen and you click to "collect" them. The playerprefs.setint call is made every time you collect it, so I dont think it would be efficient to .Save() everytime you collect something.

Any other suggestions?

avatar image roojerry · May 23, 2014 at 08:36 PM 0
Share

How about calling PlayerPrefs.Save in OnApplicationQuit(for standalone and web) and OnApplicationPause(for mobile)

avatar image kazzrberry · May 23, 2014 at 08:51 PM 0
Share

Still not working. Getting pretty frustrating :(

avatar image
0

Answer by kazzrberry · May 23, 2014 at 10:00 PM

Figured it out! So the problem was caused by the prefab being instantiated and then destroyed. All I had to do was add the following to the SunScript:

 void Start()
 {
 GameManager.totalCurrency = PlayerPrefs.GetFloat("Total Currency");
 }

basically allows the instantiated object to recognize that a number already exists in "Total Currency".

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

22 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

Related Questions

Combining two scripts... 0 Answers

Create a list of all PlayPref Keys 2 Answers

I can not seem to find where my playerprefs are saved on os x. 4 Answers

Want to make a JRPG. 1 Answer

Calling PlayerPrefs.GetInt/GetFloat every update bad for performance? 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