Hello,
I have a problem that i fight with. Im trying to make something like lootbox when player can get item but when he get one, item is removed from list and player can’t get the same thing anymore. But when i exit the game and start again list again has removed items so im trying to ad save script so program can remember witch items are removed but when i did it i have the same errors all the time.
When Save :
SerializationException: Type ‘UnityEngine.GameObject’ in Assembly ‘UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null’ is not marked as serializable.
System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers (System.RuntimeType type) (at :0)
When Load :
IOException: Sharing violation on path C:\Users\user1\AppData\LocalLow\DefaultCompany\project1\playerInfo.dat
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at :0)
{
public List<GameObject> item;
public void Loot_randoo()
{
int i = UnityEngine.Random.Range(0, item.Count);
item.SetActive(true);
item.Remove(item);
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData_2 data = new PlayerData_2();
data.item_random = item;
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
if (File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
PlayerData_2 data = (PlayerData_2)bf.Deserialize(file);
file.Close();
item = data.item_random;
}
}
}
[Serializable]
class PlayerData_2
{
public List<GameObject> item_random;
}