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 Kinger61 · May 22, 2015 at 03:23 PM · c#playerprefssavesavingvalue

Adding Value to Already Saved Values

I have this set up so that it saves the Coins Collected value and the loads it 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

 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 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;
     
     }
 
 }

Any Help Appreciated.

Comment
Add comment · Show 2
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 FortisVenaliter · May 22, 2015 at 04:28 PM 0
Share

So... what's the question?

avatar image Kinger61 · May 22, 2015 at 04:45 PM 0
Share

At the moment the coins collected is saved and can be loaded...but everytime the player plays the game and collects more coins, the coins collected value gets saved as this new value...weere as i want the new value to be added to the older saved value... $$anonymous$$g Player plays 1st time...Coins collected is 4 (this value is saved) Player plays 2nd time...Coins collected is 1 --> this value replaces the value of 4 in the previous play attempt...i want them to be added so it will equal 5...the new saved value will be 5...next time player plays they get 3...adds onto the 5 and is saved to be new value of 8 and so on ?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Morgan · May 22, 2015 at 05:47 PM

Load the old value, add the new one (stored in some variable) to it, then save the resulting sum.

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

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

Save Values On Closing of Game 1 Answer

C# Issues with either PlayerPrefs.SetVariable or UnityEvent.Invoke 1 Answer

PlayerPrefs.DeleteAll() not deleting unless app restarts 0 Answers

Multiple Cars not working 1 Answer

Unable to get save score between levels 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