Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Serializing Player Position/Rotation and Deserializing it and setting it to the player.

Discussion in 'Scripting' started by Nivalan, Aug 30, 2014.

  1. Nivalan

    Nivalan

    Joined:
    Jun 15, 2014
    Posts:
    46
    How exactly would I do this? I've been trying for a couple hours now and I can't think of a solution. I try to serialize a vector3 and a quaternion and then deserialize that and set it to the transforms position/rotation but I get the error that it can't set it because the saved data is temporary.
    My entire code:
    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 Data : MonoBehaviour {
    8.     public Combat combat;
    9.     public Professions professions;
    10.     public static Data control;
    11.     public Transform PlayerPosition;
    12.     void Awake(){
    13.         combat = GameObject.FindGameObjectWithTag("SkillGUI").GetComponent<Combat>();
    14.         professions = GameObject.FindGameObjectWithTag("SkillGUI").GetComponent<Professions>();
    15.         if(control == null){
    16.             DontDestroyOnLoad(gameObject);
    17.             control = this;
    18.         } else if(control != this){
    19.             Destroy(gameObject);
    20.         }
    21.         Load();
    22.     }
    23.     void OnApplicationQuit(){
    24.         Save();
    25.     }
    26.     public void Save(){
    27.         BinaryFormatter bf = new BinaryFormatter();
    28.         FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
    29.         PlayerData data = new PlayerData();
    30.         //levels
    31.         data.meleeLevel = combat.MeleeLevel;
    32.         data.defenseLevel = combat.DefenseLevel;
    33.         data.rangeLevel = combat.RangeLevel;
    34.         data.magicLevel = combat.MagicLevel;
    35.         data.cookingLevel = professions.CookingLevel;
    36.         data.fishingLevel = professions.FishingLevel;
    37.         data.smithingLevel = professions.SmithingLevel;
    38.         data.herbloreLevel = professions.HerbloreLevel;
    39.         data.thievingLevel = professions.ThievingLevel;
    40.         data.wcLevel = professions.WCLevel;
    41.         data.farmingLevel = professions.FarmingLevel;
    42.         //experience
    43.         data.meleeExp = combat.MeleeExp;
    44.         data.rangeExp = combat.RangeExp;
    45.         data.defenseExp = combat.DefenseExp;
    46.         data.magicExp = combat.MagicExp;
    47.         data.cookingExp = professions.CookingExp;
    48.         data.smithingExp = professions.SmithingExp;
    49.         data.farmingExp = professions.FarmingExp;
    50.         data.herbloreExp = professions.HerbloreExp;
    51.         data.thievingExp = professions.ThievingExp;
    52.         data.wcExp = professions.WCExp;
    53.         data.fishingExp = professions.FishingExp;
    54.  
    55.         bf.Serialize(file, data);
    56.         file.Close();
    57.     }
    58.     void Load(){
    59.         if(File.Exists(Application.persistentDataPath + "/playerInfo.dat")){
    60.             BinaryFormatter bf = new BinaryFormatter();
    61.             FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
    62.             PlayerData data = (PlayerData)bf.Deserialize(file);
    63.             file.Close();
    64.  
    65.             combat.MeleeLevel = data.meleeLevel;
    66.             combat.MeleeExp = data.meleeExp;
    67.             combat.DefenseLevel = data.defenseLevel;
    68.             combat.DefenseExp = data.defenseExp;
    69.             combat.RangeLevel = data.rangeLevel;
    70.             combat.RangeExp = data.rangeExp;
    71.             combat.MagicLevel = data.magicLevel;
    72.             combat.MagicExp = data.magicExp;
    73.             professions.CookingLevel = data.cookingLevel;
    74.             professions.CookingExp = data.cookingExp;
    75.             professions.FishingLevel = data.fishingLevel;
    76.             professions.FishingExp = data.fishingExp;
    77.             professions.SmithingLevel = data.smithingLevel;
    78.             professions.SmithingExp = data.smithingExp;
    79.             professions.HerbloreLevel = data.herbloreLevel;
    80.             professions.HerbloreExp = data.herbloreExp;
    81.             professions.WCLevel = data.wcLevel;
    82.             professions.WCExp = data.wcExp;
    83.             professions.ThievingLevel = data.thievingLevel;
    84.             professions.ThievingExp = data.thievingExp;
    85.         }
    86.     }
    87. }
    88.  
    89. [Serializable]
    90. class PlayerData{
    91.     //levels
    92.     public int meleeLevel;
    93.     public int rangeLevel;
    94.     public int defenseLevel;
    95.     public int magicLevel;
    96.     public int cookingLevel;
    97.     public int smithingLevel;
    98.     public int farmingLevel;
    99.     public int herbloreLevel;
    100.     public int thievingLevel;
    101.     public int wcLevel;
    102.     public int fishingLevel;
    103.     //experience
    104.     public float meleeExp;
    105.     public float rangeExp;
    106.     public float defenseExp;
    107.     public float magicExp;
    108.     public float cookingExp;
    109.     public float smithingExp;
    110.     public float farmingExp;
    111.     public float thievingExp;
    112.     public float wcExp;
    113.     public float fishingExp;
    114.     public float herbloreExp;
    115.     //player
    116.     public float PlayerX;
    117.     public float PlayerY;
    118.     public float PlayerZ;
    119. }
    120.