- Home /
Problem with SAVE/LOAD, serializationStream supports seeking, but its length is 0 and Sharing violation on path
I am getting those two errors when I run my code. I have looked a lot to try to solve the problem but all the solutions didnt work for me.
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) SaveManager.Load () (at Assets/SaveManager.cs:166) MiningOresFromMinerScript.Awake () (at Assets/Scripts/Mine/MiningOresFromMinerScript.cs:82)
IOException: Sharing violation on path /MY PATH TO MY FILE/MY PROJECT NAME/saveFile.data 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) (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare) System.IO.File.Open (System.String path, FileMode mode) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:347) SaveManager.CreateFile () (at Assets/SaveManager.cs:25) StartMinerPrefs.Awake () (at Assets/Scripts/Title/StartMinerPrefs.cs:23)
Here is my Savemanager script:
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class SaveManager : MonoBehaviour {
public static SaveManager savemanager;
void Awake()
{
savemanager = this;
// SOMEONE SAID TO ADD THIS IN >> IT DIDNT FIX IT >> Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
}
public static void CreateFile()
{
if (!File.Exists (Application.persistentDataPath + "/saveFile.data")) {
//if file doesnt exist create one
FileStream fstream = File.Create (Application.persistentDataPath + "/saveFile.data");
} else {
if (File.Exists(Application.persistentDataPath + "/saveFile.data"))
{
//if file does exist, Load Data
BinaryFormatter binary = new BinaryFormatter();
FileStream fstream = File.Open(Application.persistentDataPath + "/saveFile.data", FileMode.Open);
VarSaveManager saver = (VarSaveManager)binary.Deserialize (fstream);
fstream.Close ();
DuckCounter.gold = saver.gold;
XPManager.XP = saver.XP;
XPManager.XPCap = saver.XPCap;
XPManager.Level = saver.Level;
XPManager.XPCapMulitplier = saver.XPCapMultiplier;
XPManager.XPBarFillAmount = saver.XPBarFillAmount;
XPManager.LevelUpMoney = saver.LevelUpMoney;
XPManager.LevelUpResourceAmount = saver.LevelUpResourceAmount;
ChooseRandomBlock.Stone = saver.Stone;
ChooseRandomBlock.Coal = saver.Coal;
ChooseRandomBlock.Iron = saver.Iron;
ChooseRandomBlock.Copper = saver.Copper;
ChooseRandomBlock.Tin = saver.Tin;
ChooseRandomBlock.Amthyst = saver.Amthyst;
ChooseRandomBlock.Silver = saver.Silver;
ChooseRandomBlock.Gold = saver.Gold;
ChooseRandomBlock.Ruby = saver.Ruby;
ChooseRandomBlock.Mithril = saver.Mithril;
ChooseRandomBlock.Emerald = saver.Emerald;
ChooseRandomBlock.Diamond = saver.Diamond;
ChooseRandomBlock.Titanium = saver.Titanium;
ChooseRandomBlock.Uranium = saver.Uranium;
ChooseRandomBlock.DuckRemains = saver.DuckRemains;
ChooseRandomBlock.Steel = saver.Steel;
ChooseRandomBlock.Electrum = saver.Electrum;
//Craft
Craft.TinPanel = saver.TinPanel;
//MinerInventory Script
MinerInventoryScript.StoneMined = saver.StoneMined;
MinerInventoryScript.CoalMined = saver.CoalMined;
MinerInventoryScript.IronMined = saver.IronMined;
MinerInventoryScript.CopperMined = saver.CopperMined;
MinerInventoryScript.TinMined = saver.TinMined;
MinerInventoryScript.AmthystMined = saver.AmthystMined;
MinerInventoryScript.SilverMined = saver.SilverMined;
MinerInventoryScript.GoldOreMined = saver.GoldOreMined;
MinerInventoryScript.RubyMined = saver.RubyMined;
MinerInventoryScript.MithrilMined = saver.MithrilMined;
MinerInventoryScript.EmeraldMined = saver.EmeraldMined;
MinerInventoryScript.DiamondMined = saver.DiamondMined;
MinerInventoryScript.TitaniumMined = saver.TitaniumMined;
MinerInventoryScript.UraniumMined = saver.UraniumMined;
MinerInventoryScript.DuckRemainsMined = saver.DuckRemainsMined;
//StartMiner Prefs
StartMinerPrefs.Speed = saver.Speed;
StartMinerPrefs.UpgradeSpeed = saver.UpgradeSpeed;
StartMinerPrefs.SpeedCost = saver.SpeedCost;
StartMinerPrefs.Rarity = saver.Rarity;
StartMinerPrefs.UpgradeRarity = saver.UpgradeRarity;
StartMinerPrefs.RarityCost = saver.RarityCost;
StartMinerPrefs.Amount = saver.Amount;
StartMinerPrefs.UpgradeAmount = saver.UpgradeAmount;
StartMinerPrefs.AmountCost = saver.AmountCost;
//ALL OTHER INFO//
}
}
}
public static void Save()
{
//Save when a variable changes, make other variables static and things that need to access it access the place that created static var
//Load at start of the game save right before game quits onpause.
BinaryFormatter binary = new BinaryFormatter ();
FileStream fstream = File.Open (Application.persistentDataPath + "/saveFile.data", FileMode.Open);
VarSaveManager saver = new VarSaveManager ();
saver.gold = DuckCounter.gold;
saver.XP = XPManager.XP;
saver.XPCap = XPManager.XPCap;
saver.Level = XPManager.Level;
saver.XPCapMultiplier = XPManager.XPCapMulitplier;
saver.XPBarFillAmount = XPManager.XPBarFillAmount;
saver.LevelUpMoney = XPManager.LevelUpMoney;
saver.LevelUpResourceAmount = XPManager.LevelUpResourceAmount;
saver.Stone = ChooseRandomBlock.Stone;
saver.Coal = ChooseRandomBlock.Coal;
saver.Iron = ChooseRandomBlock.Iron;
saver.Copper = ChooseRandomBlock.Copper;
saver.Tin = ChooseRandomBlock.Tin;
saver.Amthyst = ChooseRandomBlock.Amthyst;
saver.Silver = ChooseRandomBlock.Silver;
saver.Gold = ChooseRandomBlock.Gold;
saver.Ruby = ChooseRandomBlock.Ruby;
saver.Mithril = ChooseRandomBlock.Mithril;
saver.Emerald = ChooseRandomBlock.Emerald;
saver.Diamond = ChooseRandomBlock.Diamond;
saver.Titanium = ChooseRandomBlock.Titanium;
saver.Uranium = ChooseRandomBlock.Uranium;
saver.DuckRemains = ChooseRandomBlock.DuckRemains;
saver.Steel = ChooseRandomBlock.Steel;
saver.Electrum = ChooseRandomBlock.Electrum;
//Craft
saver.TinPanel = Craft.TinPanel;
//MinerInventory Script
saver.StoneMined = MinerInventoryScript.StoneMined;
saver.CoalMined = MinerInventoryScript.CoalMined;
saver.IronMined = MinerInventoryScript.IronMined;
saver.CopperMined = MinerInventoryScript.CopperMined;
saver.TinMined = MinerInventoryScript.TinMined;
saver.AmthystMined = MinerInventoryScript.AmthystMined;
saver.SilverMined = MinerInventoryScript.SilverMined;
saver.GoldOreMined = MinerInventoryScript.GoldOreMined;
saver.RubyMined = MinerInventoryScript.RubyMined;
saver.MithrilMined = MinerInventoryScript.MithrilMined;
saver.EmeraldMined = MinerInventoryScript.EmeraldMined;
saver.DiamondMined = MinerInventoryScript.DiamondMined;
saver.TitaniumMined = MinerInventoryScript.TitaniumMined;
saver.UraniumMined = MinerInventoryScript.UraniumMined;
saver.DuckRemainsMined = MinerInventoryScript.DuckRemainsMined;
//StartMinerPrefs
saver.Speed = StartMinerPrefs.Speed;
saver.UpgradeSpeed = StartMinerPrefs.UpgradeSpeed;
saver.SpeedCost = StartMinerPrefs.SpeedCost;
saver.Rarity = StartMinerPrefs.Rarity;
saver.UpgradeRarity = StartMinerPrefs.UpgradeRarity;
saver.RarityCost = StartMinerPrefs.RarityCost;
saver.Amount = StartMinerPrefs.Amount;
saver.UpgradeAmount = StartMinerPrefs.UpgradeAmount;
saver.AmountCost = StartMinerPrefs.AmountCost;
saver.PriceTimer = Slot.PriceTimer;
fstream.Close ();
}
public static void Load()
{
if (File.Exists(Application.persistentDataPath + "/saveFile.data"))
{
BinaryFormatter binary = new BinaryFormatter();
FileStream fstream = File.Open(Application.persistentDataPath + "/saveFile.data", FileMode.Open);
VarSaveManager saver = (VarSaveManager)binary.Deserialize (fstream);
fstream.Close ();
DuckCounter.gold = saver.gold;
XPManager.XP = saver.XP;
XPManager.XPCap = saver.XPCap;
XPManager.Level = saver.Level;
XPManager.XPCapMulitplier = saver.XPCapMultiplier;
XPManager.XPBarFillAmount = saver.XPBarFillAmount;
XPManager.LevelUpMoney = saver.LevelUpMoney;
XPManager.LevelUpResourceAmount = saver.LevelUpResourceAmount;
ChooseRandomBlock.Stone = saver.Stone;
ChooseRandomBlock.Coal = saver.Coal;
ChooseRandomBlock.Iron = saver.Iron;
ChooseRandomBlock.Copper = saver.Copper;
ChooseRandomBlock.Tin = saver.Tin;
ChooseRandomBlock.Amthyst = saver.Amthyst;
ChooseRandomBlock.Silver = saver.Silver;
ChooseRandomBlock.Gold = saver.Gold;
ChooseRandomBlock.Ruby = saver.Ruby;
ChooseRandomBlock.Mithril = saver.Mithril;
ChooseRandomBlock.Emerald = saver.Emerald;
ChooseRandomBlock.Diamond = saver.Diamond;
ChooseRandomBlock.Titanium = saver.Titanium;
ChooseRandomBlock.Uranium = saver.Uranium;
ChooseRandomBlock.DuckRemains = saver.DuckRemains;
ChooseRandomBlock.Steel = saver.Steel;
ChooseRandomBlock.Electrum = saver.Electrum;
//Craft
Craft.TinPanel = saver.TinPanel;
//MinerInventory Script
MinerInventoryScript.StoneMined = saver.StoneMined;
MinerInventoryScript.CoalMined = saver.CoalMined;
MinerInventoryScript.IronMined = saver.IronMined;
MinerInventoryScript.CopperMined = saver.CopperMined;
MinerInventoryScript.TinMined = saver.TinMined;
MinerInventoryScript.AmthystMined = saver.AmthystMined;
MinerInventoryScript.SilverMined = saver.SilverMined;
MinerInventoryScript.GoldOreMined = saver.GoldOreMined;
MinerInventoryScript.RubyMined = saver.RubyMined;
MinerInventoryScript.MithrilMined = saver.MithrilMined;
MinerInventoryScript.EmeraldMined = saver.EmeraldMined;
MinerInventoryScript.DiamondMined = saver.DiamondMined;
MinerInventoryScript.TitaniumMined = saver.TitaniumMined;
MinerInventoryScript.UraniumMined = saver.UraniumMined;
MinerInventoryScript.DuckRemainsMined = saver.DuckRemainsMined;
//Miner Prefs
StartMinerPrefs.Speed = saver.Speed;
StartMinerPrefs.UpgradeSpeed = saver.UpgradeSpeed;
StartMinerPrefs.SpeedCost = saver.SpeedCost;
StartMinerPrefs.Rarity = saver.Rarity;
StartMinerPrefs.UpgradeRarity = saver.UpgradeRarity;
StartMinerPrefs.RarityCost = saver.RarityCost;
StartMinerPrefs.Amount = saver.Amount;
StartMinerPrefs.UpgradeAmount = saver.UpgradeAmount;
StartMinerPrefs.AmountCost = saver.AmountCost;
//Slot
Slot.PriceTimer = saver.PriceTimer;
//finish load and then copy to create file function.
//ALL OTHER INFO//
}
}
[Serializable]
class VarSaveManager
{
//Duck Counter
public int gold;
//XPManager
public int XP;
public int XPCap;
public int Level;
public int XPCapMultiplier;
public int XPBarFillAmount;
public int LevelUpMoney;
public int LevelUpResourceAmount;
//Slot
public int Stone;
public int Coal;
public int Iron;
public int Copper;
public int Tin;
public int Amthyst;
public int Silver;
public int Gold;
public int Ruby;
public int Mithril;
public int Emerald;
public int Diamond;
public int Titanium;
public int Uranium;
public int DuckRemains;
public int Steel;
public int Electrum;
//CRAFT
public int TinPanel;
//MINER INVENTORY SCRIPT
public int StoneMined;
public int CoalMined;
public int IronMined;
public int CopperMined;
public int TinMined;
public int AmthystMined;
public int SilverMined;
public int GoldOreMined;
public int RubyMined;
public int MithrilMined;
public int EmeraldMined;
public int DiamondMined;
public int TitaniumMined;
public int UraniumMined;
public int DuckRemainsMined;
//Miner Prefs
public int Speed;
public int UpgradeSpeed;
public int SpeedCost;
public int Rarity;
public int UpgradeRarity;
public int RarityCost;
public int Amount;
public int UpgradeAmount;
public int AmountCost;
//Shop
public float PriceTimer;
//ALL OTHER INFO//
}
void OnApplicationPause()
{
Save ();
}
}
AND Here is my MiningOresFromMinerScript:
using UnityEngine;
using System.Collections;
using System;
public class MiningOresFromMinerScript : MonoBehaviour {
DateTime currentDate;
DateTime oldDate;
public bool MinerMineTimerOn = false;
public static MiningOresFromMinerScript miningoresfromminerscript;
public float MinerMineTimer;
public string saveLocation1;
public int Speed = 20;
public int Rarity;
public int Amount;
void Update()
{
Amount = StartMinerPrefs.Amount;
Rarity = StartMinerPrefs.Rarity;
if (PlayerPrefs.GetInt ("BoughtMiner") == 1) {
if (StartMinerPrefs.Speed > 0) {
Speed = StartMinerPrefs.Speed;
}
}
if (MinerMineTimer <= 0) {
MinerMineTimer = 0;
}
if (MinerMineTimer == 0) {
MinerMineTimerOn = false;
SaveDate ();
print ("MinerMineTimer finished completing mine");
PickMinerBlock ();
}
if (MinerMineTimer == 0) {
MinerMineTimer = Speed;
print ("Timer was 0 so it was reset to Speed");
}
if (MinerMineTimerOn == true) {
CheckTime ();
MinerMineTimer -= Time.deltaTime;
print ("MinerMineTimerOn so checking date everyframe and subtracting 1 from Speed timer every time.deltatime");
}
}
void Start () {
if (MinerMineTimer <= 0) {
SaveDate ();
MinerMineTimer = 0;
}
CheckTime ();
if (MinerMineTimer > 120 && MinerMineTimer != Speed) {
MinerMineTimerOn = true;
print ("MineTimer was not at 120 so it had already started which means that timer should be On");
}
if (MinerMineTimer == 0) {
MinerMineTimer = Speed;
print ("Timer was 0 so it was reset to Speed");
}
if (PlayerPrefs.GetInt ("BoughtMiner") == 1) {
MinerMineTimerOn = true;
}
}
void Awake()
{
SaveManager.Load ();
DontDestroyOnLoad(transform.gameObject);
if (PlayerPrefs.GetInt ("BoughtMiner") == 1) {
if (StartMinerPrefs.Speed > 0) {
Speed = StartMinerPrefs.Speed;
}
}
CheckTime();
saveLocation1 = "lastSavedDate1";
}
public float CheckTime()
{
currentDate = System.DateTime.Now;
string tempString = PlayerPrefs.GetString (saveLocation1, "1");
long templong = Convert.ToInt64 (tempString);
DateTime oldDate = DateTime.FromBinary (templong);
print ("Old Date: " + oldDate);
TimeSpan difference = currentDate.Subtract (oldDate);
TimeSpan dt = TimeSpan.FromSeconds(Speed) - difference;
print ("Timer should be " + dt);
//Figure out how to subtract int and timespan// //Did that but maybe be something else setting it to 120 when game starts..
print ("Difference: " + difference);
MinerMineTimer = (float)dt.TotalSeconds;
print ("Difference is correct DateTime Checked");
return (float)difference.TotalSeconds;
}
public void SaveDate()
{
print ("Old Date Set");
PlayerPrefs.SetString (saveLocation1, System.DateTime.Now.ToBinary ().ToString ());
print ("Saving this date to player prefs: " + System.DateTime.Now);
}
private int[] values100 = {8,8,8,8,8,8,8,8,8,8,8,8,8,8 ,9,9,9,9,9,9,9,9,9,9,9, 10,10,10,10,10,10,10, 11,11,11,11, 12,12,12, 13,13,13 ,14,14,14 };
private int[] values95 = {5 ,6,6, 7,7, 8,8,8,8,8,8,8,8,8,8,8,8,8,8 ,9,9,9,9,9,9,9,9,9,9,9, 10,10,10,10,10,10,10, 11,11,11,11, 12,12,12, 13,13,13 ,14 };
private int[] values90 = {2,2,2, 3,3,3, 4,4 ,5
,6,6, 7,7,7, 8,8,8,8,8,8,8,8,8,8,8,8,8,8 ,9,9,9,9,9,9,9,9,9,9,9, 10,10,10,10,10,10,10, 11,11,11,11, 12,12,12, 13 };
private int[] values85 = {1,1, 2,2,2, 3,3,3, 4,4 ,5
,6,6, 7,7,7, 8,8,8,8,8,8,8,8,8,8,8,8,8,8 ,9,9,9,9,9,9,9,9,9,9,9, 10,10,10,10,10,10,10, 11,11,11,11, 12,12,12 };
private int[] values80 = {1,1, 2,2,2, 3,3,3, 4,4 ,5
,6,6, 7,7,7, 8,8,8,8,8,8,8,8,8,8,8,8,8,8 ,9,9,9,9,9,9,9,9,9,9,9, 10,10,10,10,10,10,10, 11,11,11,11, 12,12,12 };
private int[] values75 = {1,1, 2,2,2, 3,3,3, 4,4 ,5
,6,6, 7,7,7, 8,8,8,8,8,8,8,8,8,8,8,8,8,8 ,9,9,9,9,9,9,9,9,9,9,9, 10,10,10,10,10,10,10, 11,11,11,11, 12 };
private int[] values70 = {1,1, 2,2,2, 3,3,3, 4,4 ,5
,6,6, 7,7,7, 8,8,8,8,8,8,8,8,8,8,8 ,9,9,9,9,9,9,9,9,9, 10,10,10,10,10, 11,11,11,11, 12 };
private int[] values65 = {0,0,0, 1,1,1, 2,2,2, 3,3,3, 4,4 ,5
,6,6, 7,7,7, 8,8,8,8,8,8,8,8,8,8,8 ,9,9,9,9,9,9,9,9,9, 10,10,10,10,10, 11,11,11,11, 12 };
private int[] values60 = {0,0,0,0, 1,1,1, 2,2,2, 3,3,3, 4,4 ,5
,6,6, 7,7,7, 8,8,8,8,8,8,8,8 ,9,9,9,9,9,9,9, 10,10,10,10,10, 11,11,11,11, 12 };
private int[] values55 = {0,0,0,0,0,0,0, 1,1,1,1,1, 2,2,2,2, 3,3,3,3, 4,4,4, 5,5
,6,6, 7,7,7, 8,8,8,8,8 ,9,9,9, 10,10, 11, 12 };
private int[] values50 = {0,0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1, 2,2,2,2, 3,3,3,3, 4,4,4, 5,5
,6,6, 7,7,7, 8,8,8,8,8 ,9,9,9, 10,10, 11, 12 };
private int[] values45 = {0,0,0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,1, 2,2,2,2,2,2, 3,3,3,3,3, 4,4,4,4,4, 5,5,5,5,5,
6,6,6,6, 7,7,7, 8,8,8 };
private int[] values40 = {0,0,0,0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,1,1,1, 2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3, 4,4,4,4,4, 5,5,5,5,5,
6,6,6,6, 7,7,7, 8,8};
private int[] values35 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,11,1,1,1, 2,2,22,2,2,2,2,2,2,2, 3,3,3,3,3, 4,4,4,4,4, 5,5,5,5,5,
6,6,6,6, 7,7,7, 8,8};
private int[] values30 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,11,1,1,1,1,1, 2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3, 4,4,4,4,4, 5,5,5,5,5,
6,6,6,6, 7,7,7, 8,8};
private int[] values25 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1, 2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3, 4,4,4,4,4, 5,5,5,5,5,
6,6,6,6, 7,7,7, 8};
private int[] values20 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3, 4,4,4,4,4, 5,5,5,5,5,
6,6,6,6, 7,7,7, 8};
private int[] values15 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3, 4,4,4,4,4, 5,5,5,5,5,
6,6,6,6, 7,7,7, 8};
private int[] values10 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3, 4,4,4,4,4, 5,5,5,5,5,
6,6,6,6, 7,7,7, 8};
private int[] values5 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3, 4,4,4,4,4, 5,5,5,5,5,
6,6,6,6, 7,7,7, 8};
private int[] values0 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,1,1, 2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3, 4,4,4,4,4, 5,5,5,5,5,
6,6,6,6, 7,7,7};
private int selected;
void PickMinerBlock()
{
SaveDate ();
print ("Picking the block...");
if (Rarity == 0)
{
selected = values0 [UnityEngine.Random.Range (0, values0.Length)];
}
if (Rarity == 5)
{
selected = values5 [UnityEngine.Random.Range (0, values5.Length)];
}
if (Rarity == 10)
{
selected = values10 [UnityEngine.Random.Range (0, values10.Length)];
}
if (Rarity == 15)
{
selected = values15 [UnityEngine.Random.Range (0, values15.Length)];
}
if (Rarity == 20)
{
selected = values20 [UnityEngine.Random.Range (0, values20.Length)];
}
if (Rarity == 25)
{
selected = values25 [UnityEngine.Random.Range (0, values25.Length)];
}
if (Rarity == 30)
{
selected = values30 [UnityEngine.Random.Range (0, values30.Length)];
}
if (Rarity == 35)
{
selected = values35 [UnityEngine.Random.Range (0, values35.Length)];
}
if (Rarity == 40)
{
selected = values40 [UnityEngine.Random.Range (0, values40.Length)];
}
if (Rarity == 45)
{
selected = values45 [UnityEngine.Random.Range (0, values45.Length)];
}
if (Rarity ==50)
{
selected = values50 [UnityEngine.Random.Range (0, values50.Length)];
}
if (Rarity == 55)
{
selected = values55 [UnityEngine.Random.Range (0, values55.Length)];
}
if (Rarity == 60)
{
selected = values60 [UnityEngine.Random.Range (0, values60.Length)];
}
if (Rarity == 65)
{
selected = values65 [UnityEngine.Random.Range (0, values65.Length)];
}
if (Rarity == 70)
{
selected = values70 [UnityEngine.Random.Range (1, values70.Length)];
}
if (Rarity == 75)
{
selected = values75 [UnityEngine.Random.Range (1, values75.Length)];
}
if (Rarity == 80)
{
selected = values80 [UnityEngine.Random.Range (1, values80.Length)];
}
if (Rarity == 85)
{
selected = values85 [UnityEngine.Random.Range (1, values85.Length)];
}
if (Rarity == 90)
{
selected = values90 [UnityEngine.Random.Range (2, values90.Length)];
}
if (Rarity == 95)
{
selected = values95 [UnityEngine.Random.Range (5, values95.Length)];
}
if (Rarity == 100)
{
selected = values100 [UnityEngine.Random.Range (8, values100.Length)];
}
if (selected == 0) {
MinerInventoryScript.StoneMined = MinerInventoryScript.StoneMined + Amount;
}
if (selected == 1) {
MinerInventoryScript.CoalMined = MinerInventoryScript.CoalMined + Amount;
}
if (selected == 2) {
MinerInventoryScript.IronMined = MinerInventoryScript.IronMined + Amount;
}
if (selected == 3) {
MinerInventoryScript.CopperMined = MinerInventoryScript.CopperMined + Amount;
}
if (selected == 4) {
MinerInventoryScript.TinMined = MinerInventoryScript.TinMined + Amount;
}
if (selected == 5) {
MinerInventoryScript.AmthystMined = MinerInventoryScript.AmthystMined + Amount;
}
if (selected == 6) {
MinerInventoryScript.SilverMined = MinerInventoryScript.SilverMined + Amount;
}
if (selected == 7) {
MinerInventoryScript.GoldOreMined = MinerInventoryScript.GoldOreMined + Amount;
}
if (selected == 8) {
MinerInventoryScript.RubyMined = MinerInventoryScript.RubyMined + Amount;
}
if (selected == 9) {
MinerInventoryScript.MithrilMined = MinerInventoryScript.MithrilMined + Amount;
}
if (selected == 10) {
MinerInventoryScript.EmeraldMined = MinerInventoryScript.EmeraldMined + Amount;
}
if (selected == 11) {
MinerInventoryScript.DiamondMined = MinerInventoryScript.DiamondMined + Amount;
}
if (selected == 12) {
MinerInventoryScript.TitaniumMined = MinerInventoryScript.TitaniumMined + Amount;
}
if (selected == 13) {
MinerInventoryScript.UraniumMined = MinerInventoryScript.UraniumMined + Amount;
}
if (selected == 14) {
MinerInventoryScript.DuckRemainsMined = MinerInventoryScript.DuckRemainsMined + Amount;
}
print("Finsihed picking the block");
MinerMineTimerOn = true;
SaveManager.Save ();
}
}
AND Lastly here is my StartMinerPrefs script:
using UnityEngine; using System.Collections;
public class StartMinerPrefs : MonoBehaviour {
public static int Speed;
public static int UpgradeSpeed;
public static int SpeedCost;
public static int Rarity;
public static int UpgradeRarity;
public static int RarityCost;
public static int Amount;
public static int UpgradeAmount;
public static int AmountCost;
void Awake()
{
GameObject.FindGameObjectWithTag ("SaveManager").GetComponent<SaveManager> ();
///have to reference the gameobject the script is attached to.
SaveManager.CreateFile();
print ("Created File");
}
void Start()
{
if (Speed == 0) {
Speed = 20;
UpgradeSpeed = 15;
SpeedCost = 400;
Rarity = 0;
UpgradeRarity = 5;
RarityCost = 525;
Amount = 1;
UpgradeAmount = 2;
AmountCost = 950;
}
}
} The first script is supposed to save int values when any of those values change (There is a lot of them) and then load those values when needed, like when they are being referenced.
The second script is supposed to set up some variables so they are ready to be accessed and to load variables if the user has already modified them (through upgrades). The script also uses arrays to randomly select things and this happens based on a DateTime Timer.
The third script sets the base values for a few ints if they havent been set before by checking if one of them is 0 and if so setting them all to their base values. If the int isnt 0 then the values have already been set to the base and possibly have been modified by the player. The Awake function calls the SaveManager CreateFile() function which does check if one already exists before creating one. The reason I am calling the CreateFile in this awake function is because this script is in the very first scene so the file will always be created before it could possibly by written on to.
From what I can tell from the errors, there is a problem with my Awake Function in MiningOresFromMinerScript, my Awake Function in StartMinerPrefs, and my Load Function in SaveManger
IF You have any questions please dont be shy and ask, and i will try to get back to you as soon as possible.
Answer by 3162497047 · Mar 16, 2017 at 08:32 PM
I just solve one problem as same as your Exception. My problem is didn't use new to instance class which one I want to save But saw your code you used new to instance , so I think you maybe didn't write [Serializabe] in the front of class "public class SaveManager : MonoBehaviour "
Answer by 3162497047 · Mar 16, 2017 at 10:39 AM
Finally I think maybe you just forget use "new" to instance object in somewhere.
Because I just try to delete [Serializable] the exception became: SerializationException: Type Information is not marked as Serializable.
Answer by JackTenSeven · Mar 16, 2017 at 09:27 PM
I figured it out a while ago. I forget to serialize my data in the save function so I got the error when the load function tried to deserialize something that wasn't serialized