Editor freezing when saving in binary
I have a script which is used to save my game. Things like players coordinates, what's in their inventory, and what perks they had. However when I press the button to save while playing in the editor, it doesn't work. Unity freezes up and becomes unresponsive, I've waited several minutes and tried several times but nothing. Here is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public static class SLD {
public static void SavePlayer (Player player, Inventory inv, PerkManager pm) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream stream = new FileStream (Application.persistentDataPath + "/player.etc", FileMode.Create);
PlayerData data = new PlayerData (player, inv, pm);
bf.Serialize (stream, data);
stream.Close ();
}
public static PlayerData LoadPlayer (int saveSlot) {
if (File.Exists (Application.persistentDataPath + "/player.etc")) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream stream = new FileStream (Application.persistentDataPath + "/player.etc" + saveSlot.ToString (), FileMode.Open);
PlayerData data = bf.Deserialize (stream) as PlayerData;
stream.Close ();
foreach (Spawner spawn in Save.s.spawners) {
spawn.Save (saveSlot);
}
foreach (Container spawn in Save.s.containers) {
spawn.Save (saveSlot);
}
return data;
} else {
Debug.LogError ("File does not exsit");
return Player.p.pd;
}
}
public static void LoadAll () {
PlayerData data = LoadPlayer (0);
Player.p.water = data.stats [0];
Player.p.hunger = data.stats [1];
Player.p.sleep = data.stats [2];
Player.p.hp = data.stats [3];
Player.p.exp = data.exp;
Player.p.level = data.level;
Transform player = Player.p.player;
player.position = new Vector3 (data.x, data.y, data.z);
PerkManager.p.unlocked = data.perkUnlocked;
PerkManager.p.unlockTokens = data.perkTokens;
int i = 0;
while (i <= Inventory.inv.slots.Length) {
Inventory.inv.slots [i].slotId = data.slotIds[i];
Inventory.inv.slots [i].amount = data.slotAmounts[i];
}
}
}
[Serializable]
public class PlayerData {
public float[] stats = new float[4];
public int exp = 0;
public int level = 0;
public float x = 0;
public float y = 0;
public float z = 0;
public bool[] perkUnlocked = new bool[2];
public int perkTokens;
public int[] slotIds = new int[20];
public int[] slotAmounts = new int[20];
public PlayerData(Player player, Inventory inv, PerkManager pm) {
stats = new float[4];
stats [0] = player.water;
stats [1] = player.hunger;
stats [2] = player.sleep;
stats [3] = player.hp;
exp = player.exp;
level = player.level;
x = player.player.position.x;
y = player.player.position.y;
z = player.player.position.z;
perkUnlocked = pm.unlocked;
perkTokens = pm.unlockTokens;
int i = 0;
while (i <= inv.slots.Length) {
slotIds [i] = inv.slots [i].slotId;
slotAmounts [i] = inv.slots [i].amount;
}
}
}
Please anything helps. Thanks.
Answer by MarioSantoso · Apr 13, 2018 at 11:00 PM
You while loop can not finish because "i" never increases.
Add i++; and you should be fine.
while (i < inv.slots.Length)
{
slotIds[i] = inv.slots[i].slotId;
slotAmounts[i] = inv.slots[i].amount;
// Add this
i++;
}
This could be changed to a for() loop which will achieve but will have the counting in the definition ins$$anonymous$$d of the code block. So will become.
for(int i = 0; i < inv.slots.Length; i++)
{
slotIds[i] = inv.slots[i].slotId;
slotAmounts[i] = inv.slots[i].amount;
}
Also $$anonymous$$ilsnus made an error his response should be i++ not int++ and the while loop would need to be
while (i < inv.slots.Length)
as inv.slots[inv.slots.length] will return index out of range exception. If you have 5 items .length will return 5 which you cannot index because arrays count from 0
Your answer
![](https://koobas.hobune.stream/wayback/20220612160714im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Save Blendshape values 0 Answers
Android persistentdatapath Can be read but not written. 0 Answers
[SOLVED] "IOException: Sharing violation on path" Trying to save multiple files 4 Answers
Auto save and auto load on quit app. 1 Answer
Data not Saving in Android Using Unity Persistance Saving Training. 1 Answer