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 /
This question was closed Sep 12, 2016 at 03:56 PM by Momin1 for the following reason:

Other

avatar image
0
Question by Momin1 · Sep 11, 2016 at 04:58 PM · scripting problemserializationloadingsavingdata storage

how to properly save an int value ?

hi..i have been trying to properly save an int value using serialization following unity's data persistance tutorial but i am unable to do so.Basically i have a static int value(TotalStars) in my level selection scene and each time any of the 3 if's statements that are in script of game scenes(levels) is true the value of TotalStars is incremented by 1.Well till here it works good,i can go to first level then if i win and come back to level selection i can see the value incremented But i want to save this value and load it when we again start the game which i am unable to do so.i dont know whether its unable to save the value or i am placing the save and load method at wrong places.I have tried various things but its not working .SO can anyone tell me am i doing it the write way and where i need to put the save and load methods so they automatically work? Here is the Script from level selection scene(i tried with and without singleton following the tutorial,though i dont think i needed it )

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 Cherno · Sep 11, 2016 at 05:40 PM 0
Share

Have you tried insert Debug.Log lines to see what works and what doesn't? This should be the first step when trying to find errors.

avatar image Momin1 Cherno · Sep 11, 2016 at 06:33 PM 0
Share

yups at many places to check the value of total stars but no clue

2 Replies

  • Sort: 
avatar image
0

Answer by tkati · Sep 11, 2016 at 06:54 PM

I don't see where you are incrementing, I see a lot of data swaps but no +1 or ++ on anything ....

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 Momin1 · Sep 11, 2016 at 07:37 PM 0
Share

as i mentioned in the question detail that the value is being increased in another script so you wont see it here.didnot thought it was important to add that script also but i will include that in my comment

 void OnTriggerEnter2D (Collider2D Ball)
     {
         
         EndGame.SetActive (true);
         WinStar.SetActive (true);
         PlayerPrefs.SetInt ("Level" + CurrentLevel.ToString () + "_win", 1);    
         deactive ();
         COuntAndTime ();
         Level$$anonymous$$anager.TotalStars += 1;
avatar image
0

Answer by AurimasBlazulionis · Sep 11, 2016 at 06:53 PM

I believe this is the error:

 FileStream file = File.Create (Application.persistentDataPath + "/total.dat");

You should write it like this:

 FileStream file = File.Open (Application.persistentDataPath + "/total.dat", FileMode.OpenOrCreate);

Because now you tell Unity to create a file over existing one. If this does not work, then I would suggest moving save and load to the class, have a class instance which would store the variable, and in save method, you would not need to create additional class. bf.Serialize (file, this); would be enough.

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

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

PlayerPrefs v. XML File for Saving Data? 1 Answer

How can I load in data for a derived type class from a CSV? 0 Answers

Save/Load Animation State of Instantiated Prefabs 0 Answers

Can't Serialize Color or Vector2 3 Answers

Serialization Location 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