- Home /
 
 
               Question by 
               masteproofgamel · Jun 15, 2016 at 06:18 AM · 
                c#serializationsaveloadsave data  
              
 
              Save and load serialization problem
I have a script where I can select different character with different stats. For example warrior,mage and archer with agility, intellect and strength. I set them really easily just by taking the script component and change them like the class' are. For example if I create a warrior I say:
 Warrior_Player scriptWP = warriorCharacterPrefab.GetComponent<Warrior_Player>();
 scriptWP.strength = classSelected.strength;
 
 // This is the same for the other 2 classes so:
 
 Mage_Player scriptMP = mageCharacterPrefab.GetComponent<Mage_Player>();
 scriptMP.strength = classSelected.strength;
 
 
 Archer_Player scriptAP = archerCharacterPrefab.GetComponent<Archer_Player>();
 scriptAP.strength = classSelected.strength;
 
 
               In the save and load serialization script i have this:
 using UnityEngine;
 using System.Collections;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 using System;
 public class Save_And_Load : MonoBehaviour {
     public static void Save()
     {
         BinaryFormatter binaryFormatter = new BinaryFormatter();
         FileStream file = File.Create(Application.persistentDataPath + "/playerData.dat");
         PlayerData playerData = new PlayerData();
         // I need to set the stats
         // So if the warriorCharacterPrefab is created:
         // playerData.forza = warriorCharacterPrefab.strength;
         // But i dont know how I can do this.
         // PLS HELP
         // binaryFormatter.Serialize(file, stats)
         file.Close();
     }
     public static void Load()
     {
         if (File.Exists(Application.persistentDataPath + "/playerData.dat"))
         {
             BinaryFormatter binaryFormatter = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/playerData.dat", FileMode.Open);
             binaryFormatter.Deserialize(file);
             // characterCreated.strength = playerData.forza
             file.Close();
         }
     }
 }
 [Serializable]
 class PlayerData
 {
     public string nomeGiocatore; // Name
     public BaseCharacterClass classeGiocatore; // Class
     public int livelloGiocatore; // Level
     // Health
     public int vitaMassima;
     public int vitaAttuale;
     public int vitaMinima;
     // Stamina
     public int energiaMassima;
     public int energiaAttuale;
     public int energiaMinima;
     public int forza;  // Strength
     public int intelligenza; // Intellect
     public int destrezza; // Agility
 }
 
               So I just need someone who tells me how can I set the playerData.forza ( strength ) as it is in the class created.
               Comment
              
 
               
              Your answer