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 /
avatar image
0
Question by khitjuan2629 · Nov 12, 2018 at 03:04 PM · serializationsave datasaveloadsave-to-filesave file

saving array to bin file

I'm doing this save/load script in the quickest way possible because of my school deadlines. my problem is i'm having these errors:


 SerializationException: serializationStream supports seeking, but its length is 0
 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:155)
 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:136)
 GameControl.OnEnable () (at Assets/Scripts/GameControl.cs:146)
 
 
 NullReferenceException: Object reference not set to an instance of an object
 GameControl.OnDisable () (at Assets/Scripts/GameControl.cs:70)
 GameControl.OnApplicationFocus (Boolean hasFocus) (at Assets/Scripts/GameControl.cs:138)
 UnityEditorInternal.InternalEditorUtility:OnGameViewFocus(Boolean)
 UnityEditor.HostView:OnLostFocus()
 
 IOException: Sharing violation on path C:\Users\XXX\AppData\LocalLow\XXX\Swarm\PlayerData.eut
 System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:320)
 System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
 (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int)
 System.IO.File.Create (System.String path, Int32 bufferSize) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:135)
 System.IO.File.Create (System.String path) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:130)
 GameControl.OnDisable () (at Assets/Scripts/GameControl.cs:61)
 



i'm really new at coding, so i need help with the script. it's messy and inefficient, but please bare with me lol.


 using UnityEngine;
 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public class GameControl : MonoBehaviour {
 
   public static GameControl gameControl;
 
   [Header("Ad counters")]
   public int gameCounter;
   public int showAdCounter = 3;
 
   [Header ("Store Variables")]
   public float coins;
   public float[] playerDesign;
   public int hairColorsAvailable = 9;
   public bool[] isHairColorPurchased;
   public int shirtColorsAvailable = 9;
   public bool[] isShirtColorPurchased;
   public int skinColorsAvailable = 9;
   public bool[] isSkinColorPurchased;
   public float[] zombieDesign;
   public int zombieSkinColorsAvailable = 9;
   public bool[] isZombieSkinColorPurchased;
   public int zombieShirtColorsAvailable = 9;
   public bool[] isZombieShirtColorPurchased;
   public int weaponEquipped;
   public int weaponsAvailable = 9;
   public bool[] isWeaponPurchased;
 
   [Header("Time Variables")]
   public DateTime currentDate;
 
   [Header("Settings Variables")]
   public float musicPercentage = 0.5f;
   public float sfxPercentage = 0.5f;
 
   [Header ("Records")]
   public int bestKillCount;
 
   [Header("Free coins per update")]
   public int versionNumber;
   public int newVersionNumber;
   public bool isRewardClaimed;
 
   void Start(){
     playerDesign = new float[3];
     zombieDesign = new float[2];
     isHairColorPurchased = new bool[hairColorsAvailable];
     isShirtColorPurchased = new bool[shirtColorsAvailable];
     isSkinColorPurchased = new bool[skinColorsAvailable];
     isZombieSkinColorPurchased = new bool[zombieSkinColorsAvailable];
     isZombieShirtColorPurchased = new bool[zombieShirtColorsAvailable];
     isWeaponPurchased = new bool[weaponsAvailable];
   }
 
   void OnDisable(){ /*save data*/
     BinaryFormatter bf = new BinaryFormatter();
     FileStream file = File.Create(Path.Combine(Application.persistentDataPath, "PlayerData.eut"));
 
     PlayerData data = new PlayerData();
 
     /*insert data here to save*/
     data.coins = coins;
 
     int x = 0;
     while (x < playerDesign.Length){
       data.playerDesign[x] = playerDesign[x];
       x++;
     }
     x = 0;
     while (x < hairColorsAvailable){
       data.isHairColorPurchased[x] = isHairColorPurchased[x];
       x++;
     }
     x = 0;
     while (x < hairColorsAvailable){
       data.isShirtColorPurchased[x] = isShirtColorPurchased[x];
       x++;
     }
     x = 0;
     while (x < shirtColorsAvailable){
       data.isShirtColorPurchased[x] = isShirtColorPurchased[x];
       x++;
     }
     x = 0;
     while (x < skinColorsAvailable){
       data.isSkinColorPurchased[x] = isSkinColorPurchased[x];
       x++;
     }
     x = 0;
     while (x < 2){
       data.zombieDesign[x] = zombieDesign[x];
       x++;
     }
     x = 0;
     while (x < zombieShirtColorsAvailable){
       data.isZombieShirtColorPurchased[x] = isZombieShirtColorPurchased[x];
       x++;
     }
     x = 0;
     while (x < zombieSkinColorsAvailable){
       data.isZombieSkinColorPurchased[x] = isZombieSkinColorPurchased[x];
       x++;
     }
     x = 0;
     while (x < weaponsAvailable){
       data.isWeaponPurchased[x] = isWeaponPurchased[x];
       x++;
     }
 
     data.weaponEquipped = weaponEquipped;
 
     data.currentDate = currentDate;
 
     data.musicPercentage = musicPercentage;
     data.sfxPercentage = sfxPercentage;
 
     data.bestKillCount = bestKillCount;
 
     data.versionNumber = versionNumber;
     data.isRewardClaimed = isRewardClaimed;
 
     bf.Serialize(file, data);
     file.Close();
   }
 
   void OnApplicationPause(bool pauseStatus){
     if (pauseStatus){
       OnDisable();
     }
   }
 
   void OnApplicationFocus(bool hasFocus){
     if (!hasFocus){
       OnDisable();
     }
   }
 
   void OnEnable(){ /*load data*/
     Start();
     BinaryFormatter bf = new BinaryFormatter();
     FileStream file = File.Open(Path.Combine(Application.persistentDataPath, "PlayerData.eut"), FileMode.Open);
     PlayerData data = (PlayerData)bf.Deserialize(file);
     file.Close();
 
     /*transfer data here*/
     coins = data.coins;
 
     int x = 0;
     while (x < 3){
       playerDesign[x] = data.playerDesign[x];
       x++;
     }
     x = 0;
     while (x < hairColorsAvailable){
       isHairColorPurchased[x] = data.isHairColorPurchased[x];
       x++;
     }
     x = 0;
     while (x < hairColorsAvailable){
       isShirtColorPurchased[x] = data.isShirtColorPurchased[x];
       x++;
     }
     x = 0;
     while (x < shirtColorsAvailable){
       isShirtColorPurchased[x] = data.isShirtColorPurchased[x];
       x++;
     }
     x = 0;
     while (x < skinColorsAvailable){
       isSkinColorPurchased[x] = data.isSkinColorPurchased[x];
       x++;
     }
     x = 0;
     while (x < 2){
       zombieDesign[x] = data.zombieDesign[x];
       x++;
     }
     x = 0;
     while (x < zombieShirtColorsAvailable){
       isZombieShirtColorPurchased[x] = data.isZombieShirtColorPurchased[x];
       x++;
     }
     x = 0;
     while (x < zombieSkinColorsAvailable){
       isZombieSkinColorPurchased[x] = data.isZombieSkinColorPurchased[x];
       x++;
     }
     x = 0;
     while (x < weaponsAvailable){
       isWeaponPurchased[x] = data.isWeaponPurchased[x];
       x++;
     }
 
     weaponEquipped = data.weaponEquipped;
 
     currentDate = data.currentDate;
 
     musicPercentage = data.musicPercentage;
     sfxPercentage = data.sfxPercentage;
 
     bestKillCount = data.bestKillCount;
 
     versionNumber = data.versionNumber;
     isRewardClaimed = data.isRewardClaimed;
   }
 
     void Awake(){
     Screen.sleepTimeout = SleepTimeout.NeverSleep;
     if (gameControl == null){
         DontDestroyOnLoad(gameObject);
       gameControl = this;
     }
     else if (gameControl != this){
       Destroy(gameObject);
     }
   }
 
 }
 
 [Serializable]
 class PlayerData{
   public float coins;
   public float[] playerDesign;
   public bool[] isHairColorPurchased;
   public bool[] isShirtColorPurchased;
   public bool[] isSkinColorPurchased;
   public float[] zombieDesign;
   public bool[] isZombieSkinColorPurchased;
   public bool[] isZombieShirtColorPurchased;
   public int weaponEquipped;
   public bool[] isWeaponPurchased;
 
   public DateTime currentDate;
 
   public float musicPercentage = 0.5f;
   public float sfxPercentage = 0.5f;
 
   public int bestKillCount;
 
   public int versionNumber;
   public bool isRewardClaimed;
 }
 



need help, thanks!

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

0 Replies

· Add your reply
  • Sort: 

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

96 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

Related Questions

PlayerPrefs not saving in build 1 Answer

How do i change my save "Total Coin" everytime i get more coins. 0 Answers

Persisting data between scenes: can't find an answer 2 Answers

Deserializing data with XML Serializer 0 Answers

How do I save a List in my project? 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