Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by clintbaucom · Aug 25, 2018 at 02:53 PM · arrayserializationsaveload

Loading and Saving a Serializable Array.

I'm not receiving any errors in Unity, but Data is not saving or loading. using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using UnityEngine.UI; using System.Runtime.Serialization.Formatters.Binary; using System.IO;

public class SavedScore : MonoBehaviour { public static SavedScore score;

 public int[] lvlScore;

 public void Save(int score)
 {
     lvlScore[GameControl.control.currentLevel] = score;
     BinaryFormatter binaryFormatter = new BinaryFormatter();
     FileStream file = File.Create(Application.persistentDataPath + "/FWScore.dat");
     FWScore data = new FWScore();
     for (int i = 0; i == 199; i++){
         data.lvlScore[i] = lvlScore[i];
     }



     binaryFormatter.Serialize(file, data);
     file.Close();

 }

 public void Load()
 {
     if (File.Exists(Application.persistentDataPath + "/FWScore.dat"))
     {
         BinaryFormatter binaryFormatter = new BinaryFormatter();
         FileStream file = File.Open(Application.persistentDataPath + "/FWScore.dat", FileMode.Open);
         FWScore data = (FWScore)binaryFormatter.Deserialize(file);
         file.Close();
         for (int i = 0; i == 199; i++)
         {
             lvlScore[i] = data.lvlScore[i];
         }



     }
 }

}

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 eses · Aug 26, 2018 at 09:42 AM 0
Share

@clintbaucom - Have you already solved this? If not, you seem to have couple of problems....

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by eses · Aug 26, 2018 at 11:20 AM

Hi @clintbaucom

Your script has some issues that would have been solved by reading and debugging the code by printing out the contents of your saved data / array.

You don't show serializable FWScore class, but I expect it just mirrors lvlScore array:

 [System.Serializable]
 public class FWScore 
 {
     public int[] lvlScore;
 }


First you have this:

      FWScore data = new FWScore();
      for (int i = 0; i == 199; i++){
          data.lvlScore[i] = lvlScore[i];
      }


Your FWScore contains most like an array, so you should initialize it's size. Also your loop condition is bit strange. What if you don't have exactly this amount of values in use? Maybe make it match lvlScore length?

 FWScore data = new FWScore();
 
 data.lvlScore = new int[lvlScore.Length];
 
 for (int i = 0; i < lvlScore.Length; i++)
 {
     data.lvlScore[i] = lvlScore[i];
 }


Then the Load() method. Here you could just first do:

   if (File.Exists(Application.dataPath + "/FWScore.dat") == false)
             return;


Here too, you should initialize lvlScore array in this class, when loading the data in:

 lvlScore = new int[data.lvlScore.Length];
 
 for (int i = 0; i < data.lvlScore.Length; i++)
 {
     lvlScore[i] = data.lvlScore[i];
 }


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 Rem777 · Jun 26, 2021 at 06:06 AM 0
Share

thank you! its helped me

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

170 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 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 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 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 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 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 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 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

Unity custom class array serialization issue. 0 Answers

Custom Class - What did I do wrong? 0 Answers

Debugging Save / Load Glitches? (Serialization) 1 Answer

Change serialized array size 1 Answer

problem with loading data with serialization in the right time 0 Answers


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