I have a code that saves data in the data file are saved without any problem.But I also tried to save an array but the array was not saved and I get an error.I keep the data (and also the array) in BinaryFormatter.The error indicates the line bf.Serialize (file, data) before closing the file in the save function.
the code
public class SaveLoadScript : MonoBehaviour
{
public int allMoneyOfplayer, allXpOfPlayer, laval, maxXp, howMuchUpMaxXp;
public float highTime;
public List<GunScript> gun = new List<GunScript>();
public void Start()
{
Save();
}
public void Update()
{
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath "/saveG.file");
PlayerData data = new PlayerData();
data.money = allMoneyOfplayer;
data.xp = allXpOfPlayer;
if (highTime > data.time)
data.time = highTime;
data.laval = laval;
data.maxXp = maxXp;
data.howMuchUpMaxXp = howMuchUpMaxXp;
for (int i = 0; i < gun.Count; i )
data.gun.Add(gun[i]);
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
if (File.Exists(Application.persistentDataPath "/saveG.file"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath "/saveG.file", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
allMoneyOfplayer = data.money;
allXpOfPlayer = data.xp;
highTime = data.time;
laval = data.laval;
maxXp = data.maxXp;
howMuchUpMaxXp = data.howMuchUpMaxXp;
file.Close();
}
}
}
[System.Serializable]
class PlayerData
{
public int money;
public int xp;
public float time;
public int laval;
public int maxXp;
public int howMuchUpMaxXp;
public List<GunScript> gun = new List<GunScript>();
}
the error
SerializationException: Type 'GunScript' in Assembly 'Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers (System.RuntimeType type) (at :0)
System.Runtime.Serialization.FormatterServices <>c__DisplayClass9_0.b__0 (System.Runtime.Serialization.MemberHolder _) (at :0)
System.Collections.Concurrent.ConcurrentDictionary2[TKey,TValue].GetOrAdd (TKey key, System.Func
2[T,TResult] valueFactory) (at :0)
System.Runtime.Serialization.FormatterServices.GetSerializableMembers (System.Type type, System.Runtime.Serialization.StreamingContext context) (at :0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo () (at :0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize (System.Type objectType, System.Runtime.Serialization.ISurrogateSelector surrogateSelector, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.Formatters.Binary.SerObjectInfoInit serObjectInfoInit, System.Runtime.Serialization.IFormatterConverter converter, System.Runtime.Serialization.SerializationBinder binder) (at :0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize (System.Type objectType, System.Runtime.Serialization.ISurrogateSelector surrogateSelector, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.Formatters.Binary.SerObjectInfoInit serObjectInfoInit, System.Runtime.Serialization.IFormatterConverter converter, System.Runtime.Serialization.SerializationBinder binder) (at :0)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArray (System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo objectInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo memberNameInfo, System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo memberObjectInfo) (at :0)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write (System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo objectInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo memberNameInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo typeNameInfo) (at :0)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize (System.Object graph, System.Runtime.Remoting.Messaging.Header[] inHeaders, System.Runtime.Serialization.Formatters.Binary.__BinaryWriter serWriter, System.Boolean fCheck) (at :0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers, System.Boolean fCheck) (at :0)
CodePudding user response:
public class GunScript : MonoBehaviour
{
public bool bayYesOrNo = false;
public int gunAttack, sill;
Vector2 mousePosition;
Vector2 directionMouse;
float spasBetweenBuiltToBuilt;
public GameObject built;
public Transform honestOfGun;
public Rigidbody2D rb;
public Rigidbody player;
// Start is called before the first frame update
void Start()
{
spasBetweenBuiltToBuilt = 0f;
}
// Update is called once per frame
void Update()
{
FollowAfterMouse();
Shoot();
}
void Shoot()
{
spasBetweenBuiltToBuilt = 1 * Time.deltaTime;
if (Input.GetMouseButton(0) && spasBetweenBuiltToBuilt >= 0.2)
{
Instantiate(built, honestOfGun.position, honestOfGun.rotation);
spasBetweenBuiltToBuilt = 0f;
}
}
void FollowAfterMouse()
{
mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
directionMouse = new Vector2(mousePosition.x - rb.position.x, mousePosition.y - rb.position.y);
float angle = Mathf.Atan2(directionMouse.x, directionMouse.y) * Mathf.Rad2Deg - 90f;
rb.transform.rotation = Quaternion.Euler(0, 0, -angle);
rb.position = player.position;
}
}
CodePudding user response:
[System.Serializable] //<-- add this attribute
public class GunScript