- Home /
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