Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Code issues with Saving data, Serializable,C#

Discussion in 'Scripting' started by Snicky, Jul 18, 2015.

  1. Snicky

    Snicky

    Joined:
    Mar 5, 2015
    Posts:
    36
    I have been trying to setup a script that saves and loads using Serializable.This is what i got so far

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. using System.Runtime.Serialization.Formatters.Binary;
    5. using System.IO;
    6.  
    7. public class MyGameSettings : MonoBehaviour {
    8.  
    9.     public static MyGameSettings myGameSettings;
    10.  
    11.     public string _name;                            
    12.     public int _stage;                                    
    13.    
    14.     public Attribute[] _primaryAttributes;
    15.     public MyVital[] _vitals;
    16.     public MyCharacterSkills[] _skills;                  
    17.     public MyTalent[] _talents;
    18.  
    19.     public Sprite selectedSprite;
    20.  
    21.  
    22.     void Awake()
    23.     {
    24.         if (myGameSettings == null) {
    25.  
    26.             DontDestroyOnLoad (gameObject);
    27.             myGameSettings = this;
    28.         } else if (myGameSettings != this) {
    29.             Destroy (gameObject);
    30.         }
    31.         SetupPrimaryAttributes ();
    32.     }
    33.  
    34.     // Use this for initialization
    35.     void Start () {
    36.    
    37.     }
    38.    
    39.     // Update is called once per frame
    40.     void Update () {
    41.  
    42.     }
    43.  
    44.     private void SetupPrimaryAttributes(){
    45.      _primaryAttributes = new Attribute[Enum.GetValues(typeof(AttributeName)).Length];
    46.         for (int cnt = 0; cnt <_primaryAttributes.Length; cnt++) {
    47.             _primaryAttributes [cnt] = new Attribute ();
    48.  
    49.             _primaryAttributes [cnt].Name = ((AttributeName)cnt).ToString ();
    50.         }
    51.  
    52.         //for serialization
    53.         PlayerData data = new PlayerData ();
    54.         data.attribute = new Attribute ();
    55.         data._primaryAttributes = new Attribute[Enum.GetValues(typeof(AttributeName)).Length];
    56.         for (int cnt = 0; cnt <_primaryAttributes.Length; cnt++) {
    57.             _primaryAttributes [cnt] = data.attribute;
    58.             //_primaryAttributes [cnt].Name = ((AttributeName)cnt).ToString ();
    59.         }
    60.  
    61.     }
    62.  
    63.     public void SaveMyCharacterData(){
    64.         GameObject pc = GameObject.Find ("pc");
    65.         PlayerCharacter pcClass = pc.GetComponent<PlayerCharacter> ();
    66.         _name = pcClass.Name;
    67.         _stage = 1;
    68.  
    69.         for (int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
    70.  
    71.             _primaryAttributes[cnt].BaseValue    = pcClass.GetPrimaryAttribute (cnt).BaseValue;
    72.             Debug.Log (_primaryAttributes[cnt].Name);
    73.             Debug.Log(_primaryAttributes[cnt].BaseValue);
    74.  
    75.         }
    76.  
    77.         BinaryFormatter bf = new BinaryFormatter ();
    78.         FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");
    79.  
    80.         PlayerData data = new PlayerData ();
    81.         data._name = _name;
    82.         data._primaryAttributes = _primaryAttributes;
    83.         bf.Serialize (file, data);
    84.         file.Close ();
    85.     }
    86.  
    87.     public void Load()
    88.     {
    89.         if(File.Exists(Application.persistentDataPath +"/playerInfo.dat"))
    90.         {
    91.                 BinaryFormatter bf = new BinaryFormatter();
    92.                 FileStream file = File.Open(Application.persistentDataPath +"/playerInfo.dat",FileMode.Open);
    93.             PlayerData data = (PlayerData)bf.Deserialize(file);
    94.             file.Close();
    95.  
    96.             _name = data._name;
    97.             _primaryAttributes = data._primaryAttributes;
    98.          
    99.     }
    100.  
    101. }
    102. [Serializable]
    103. class PlayerData
    104. {
    105.     public string _name;                                
    106.     public int _stage;                                    
    107.     public Attribute[] _primaryAttributes;
    108.     public Attribute attribute;
    109. }
    110. }
    111.  
    112.  
    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
     
  2. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    Snicky likes this.
  3. HandOfPriam

    HandOfPriam

    Joined:
    Aug 19, 2010
    Posts:
    34
    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.
     
    Snicky likes this.
  4. Snicky

    Snicky

    Joined:
    Mar 5, 2015
    Posts:
    36
    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.:D