after following the a unity tutorial I keep getting these SerializationException errors…
SerializationException: Type
‘PlayerData’ in Assembly
‘Assembly-CSharp, Version=0.0.0.0,
Culture=neutral, PublicKeyToken=null’
is not marked as serializable.
IOException: Sharing violation on path
C:\Users\my
PC\AppData\LocalLow\DefaultCompany
ewscene\playerInfo.dat
SerializationException: End of Stream
encountered before parsing was
completed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class saveStuff : MonoBehaviour
{
public static saveStuff control;
[SerializeField]
public int health;
void Awake()
{
if (control == null)
{
DontDestroyOnLoad(gameObject);
control = this;
}
else if(control != this)
{
Destroy(gameObject);
}
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData() { };
data.health = health;
Debug.Log("Save");
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 data = (PlayerData)bf.Deserialize(file);
health = data.health;
file.Close();
Debug.Log("LOAD");
}
else
{
Debug.Log("no file found! :-( ");
}
}
}
[SerializeField]
public class PlayerData
{
public int health;
}
what’s going on??? I’ve tried making it work for all day but i can’t figure it out!?