Code issues with Saving data, Serializable,C#

I have been trying to setup a script that saves and loads using Serializable.This is what i got so far

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public class MyGameSettings : MonoBehaviour {

    public static MyGameSettings myGameSettings;

    public string _name;                             
    public int _stage;                                    
   
    public Attribute[] _primaryAttributes;
    public MyVital[] _vitals;
    public MyCharacterSkills[] _skills;                   
    public MyTalent[] _talents;

    public Sprite selectedSprite;


    void Awake()
    {
        if (myGameSettings == null) {

            DontDestroyOnLoad (gameObject);
            myGameSettings = this;
        } else if (myGameSettings != this) {
            Destroy (gameObject);
        }
        SetupPrimaryAttributes ();
    }

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {

    }

    private void SetupPrimaryAttributes(){
     _primaryAttributes = new Attribute[Enum.GetValues(typeof(AttributeName)).Length];
        for (int cnt = 0; cnt <_primaryAttributes.Length; cnt++) {
            _primaryAttributes [cnt] = new Attribute ();

            _primaryAttributes [cnt].Name = ((AttributeName)cnt).ToString ();
        }

        //for serialization
        PlayerData data = new PlayerData ();
        data.attribute = new Attribute ();
        data._primaryAttributes = new Attribute[Enum.GetValues(typeof(AttributeName)).Length];
        for (int cnt = 0; cnt <_primaryAttributes.Length; cnt++) {
            _primaryAttributes [cnt] = data.attribute;
            //_primaryAttributes [cnt].Name = ((AttributeName)cnt).ToString ();
        }

    }

    public void SaveMyCharacterData(){
        GameObject pc = GameObject.Find ("pc");
        PlayerCharacter pcClass = pc.GetComponent<PlayerCharacter> ();
        _name = pcClass.Name;
        _stage = 1;

        for (int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {

            _primaryAttributes[cnt].BaseValue    = pcClass.GetPrimaryAttribute (cnt).BaseValue;
            Debug.Log (_primaryAttributes[cnt].Name);
            Debug.Log(_primaryAttributes[cnt].BaseValue);

        }

        BinaryFormatter bf = new BinaryFormatter ();
        FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");

        PlayerData data = new PlayerData ();
        data._name = _name;
        data._primaryAttributes = _primaryAttributes;
        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);
            file.Close();

            _name = data._name;
            _primaryAttributes = data._primaryAttributes;
         
    }

}
[Serializable]
class PlayerData
{
    public string _name;                                
    public int _stage;                                    
    public Attribute[] _primaryAttributes;
    public Attribute attribute;
}
}

This script is supposed to gather all information and then save and load it. The problem I have is when I deal with non built in type that require Initialization first. String and int types work, but I cannot get it right with my Attribute[ ] type. I get Serial.Exception : Type Attribute not marked as Serializable. I believe my error is in line 54. Rather than = new Attribute (); I maybe need to address the public Attribute attribute in my class PlayerData, wich I dont know how to do.

Maybe Im completly on the wrong track. If someone knows how to solve this, pls let me know. Thank you

I just made post about serialization and encryption like 20 minutes ago.

You might check it out and copy code you need as it is very easy (for big and small amounts of data).

There is conclusion if you don’t have much time.

Hope it helps

1 Like

You’re including System, which has an Attribute class. The serializer may be trying to serialize that, but that Attribute class in System is not serializable. That may be the cause of your error.

To specify, System.Attribute is a class, and the compiler may assume you’re using that class, not your own defined Attribute class.

1 Like

Hi, thanks for your input. I’ve used phodas suggested saving methods and put all data into a string first. So I dont have to deal with my problem anymore.In the future I will avoid naming my classes Attribute though.:smile: