• 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
Question by Kinger61 · May 20, 2015 at 02:07 PM · c#playerprefssavingvalue

Save Values On Closing of Game

I have a simple scene set up that the player walks around and collects coins. Im using player prefs setInt to store the value of CoinsCollected and then player prefs getInt to get that saved value so it can be displayed on a different scene (The win screen). The game is an android build which is put onto a android tablet for testing purposes. I want the saved value to be saved so if the user quits the game and then later returns to it, the value will b available to be shown when the game loads. How would you go about getting the saved value to stay saved on closing of the game so it can be shown and changed when reopened ? Any help would be great, thanks.

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by HarshadK · May 20, 2015 at 02:15 PM

Go through this tutorial video: Persistence - Saving and Loading Data

Comment
Kinger61
DanGoldstein91
abhi_360

People who like this

3 Show 4 · 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 Kinger61 · May 21, 2015 at 04:32 PM 0
Share

I have looked through tutorial and tried to implement the Save and Load that he uses. I however need the game to auto load the saved info when the game starts up and to be displayed in a different scene from the title scene which loads first. The data to be saved needs to be saved when the player dies so that shouldnt be a problem by adding it to the code before loading the scene after the player dies. The problem im having is the variables such as Coins, Score are in scripts within the game...I need to access them variables in different scenes from different scripts so that the Save function can be as mentioned, when player dies in main scene, and Load function to be used as game loads up ?

avatar image HarshadK · May 22, 2015 at 05:39 AM 0
Share

You can write a data manager script with a singleton that will persist through the scenes. The method explained in that video with DontDestroyOnLoad.

This data manager can keep track of coins, score etc. You can load the values in the Start() method of this data namanger. Also whenever these values change you can set these required values in the data manager. And you can have a save method in your data manager to save the data that you can call when the player dies or for efficiency when the data manager gets destroyed.

avatar image abhi_360 · May 22, 2015 at 05:51 AM 1
Share

Try watching the video a few more times and go through stack overflow on data persistence. I had the same problem in the beginning and believe me that video has all your answers you just need to use it in application with more understanding for your requirements

avatar image Kinger61 · May 22, 2015 at 02:33 PM 0
Share

Thanks guys, great help! Got it working so that it saves the Coins Collected value and the loads in in another scene. Ill attach the two scripts below...What i want to do now is so the ObjectDestroy class will save its value as Save_Coins_Collected but when it saves the value, I want to add this to the previously saved value which would be loaded in the Upgrades_Script as TotalCoinsCollected. This will then be so the player collects coins and if they play again or close game and then play later, it will add the coins to there previous amount of coins. The coins will build up and allow them to buy things in game.

ObjectDestroy.cs public class ObjectDestroy : MonoBehaviour {

     void OnTriggerEnter(Collider other) //When a named trigger is entered...do something
     {
         
         if (other.tag == "ObjectDestroy") 
         {
             Debug.Log ("HITTTTTTTTT"); //display in console if object hits the ObjectDestroy tag
             Destroy (gameObject); // destroy object this script is attached to
         }
 
 
         if (other.tag == "Player")
         {
             Lives_2.Lives_Left --;
             Time.timeScale = 0; // stop the game if player tag is hit
 
             CoinsCollected.Coins_Collected = PlayerPrefs.GetInt ("Coins");
 
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");
             
             PlayerData data = new PlayerData();
             data.Save_Coins_Collected = CoinsCollected.Coins_Collected;
             
             bf.Serialize (file, data);
             file.Close();
 
             Application.LoadLevel ("Lose_Screen"); //Load Lose_Screen scene
         }
     }
 
 }
 
 [Serializable]
 class PlayerData
 {
     public int Save_Coins_Collected;
 }

Upgrades_Script.cs public class Upgrades_Script : MonoBehaviour {

     public static int TotalCoinsCollected;
 
     Text text;
     
     void Awake()
     {
         text = GetComponent<Text> ();
     }
 
     // Use this for initialization
     void Start () 
     {
 
         if (File.Exists (Application.persistentDataPath + "/playerInfo.dat")) 
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
             PlayerData data = (PlayerData)bf.Deserialize(file);
             file.Close();
             
             TotalCoinsCollected = data.Save_Coins_Collected;
         }
 
         text.text = "Total Coins Collected: " + TotalCoinsCollected;
     }
 }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Adding Value to Already Saved Values 1 Answer

C# How To Save PlayerPrefs for Unity GUI 2 Answers

onapplicationquit() function is not working with unity 4.6.3 (android) 3 Answers

Invalid floats in PlayerPrefs 3 Answers

Multiple Cars not working 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