- Home /
How do I assign serialized data, to my player script?
Good day everybody. I have been making a saving platform for my game. The problem is I am not sure if my data even gets saved or deserialized. On top of that I cant get the values to apply in the inspector when I press load. I also can not get those value to apply to my player script. for instance:
health = data.health; Player.GetComponent().Health = health;
makes logically sense in theory... DOES NOT WORK. haha here is my full script.
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using PaxzifiziaEngineAssets.Characters.ThirdPerson;
public class gamePrefs : MonoBehaviour
{
public static gamePrefs control;
[Header("Player References")]
public GameObject Player;
public float health;
public float maxHealth;
public float hunger;
public float maxHunger;
public float thirst;
public float maxThirst;
public float maxLevel;
public float playerLevel;
public float levelProg;
public float maxLevelProg;
public float survivalLevel;
public float survivalLevelProg;
public float maxSurvivalLevelProg;
public float staminaLevel;
public float staminaProg;
public float maxStaminaProg;
public float stamina;
public float maxStamina;
public Vector3 startPos;
public Quaternion startRot;
void Start()
{
Player = GameObject.FindGameObjectWithTag("Player");
if (control == null)
{
DontDestroyOnLoad(gameObject);
control = this;
}
else if (control != this)
{
Destroy(gameObject);
}
}
public void Save()
{
health = Player.GetComponent<ThirdPersonCharacter>().Health;
maxHealth = Player.GetComponent<ThirdPersonCharacter>().maxHealth;
hunger = Player.GetComponent<ThirdPersonCharacter>().Hunger;
maxHunger = Player.GetComponent<ThirdPersonCharacter>().maxHunger;
thirst = Player.GetComponent<ThirdPersonCharacter>().Thirst;
maxThirst = Player.GetComponent<ThirdPersonCharacter>().maxThirst;
playerLevel = Player.GetComponent<ThirdPersonCharacter>().level;
staminaLevel = Player.GetComponent<PlayerAttack>().staminaLevel;
staminaProg = Player.GetComponent<PlayerAttack>().staminaStats;
stamina = Player.GetComponent<PlayerAttack>().stamina;
maxStamina = Player.GetComponent<PlayerAttack>().maxStamina;
startRot = Player.GetComponent<respawn>().startRot;
startPos = Player.GetComponent<respawn>().startPos;
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerData.dat");
PlayerData data = new PlayerData();
data.health = health;
data.maxHealth = maxHealth;
data.hunger = hunger;
data.maxHunger = maxHunger;
data.thirst = thirst;
data.maxThirst = maxThirst;
data.maxLevel = maxLevel;
data.playerLevel = playerLevel;
data.maxLevelProg = maxLevelProg;
data.levelProg = levelProg;
data.survivalLevel = survivalLevel;
data.survivalLevelProg = survivalLevelProg;
data.maxSurvivalLevelProg = maxSurvivalLevelProg;
data.staminaLevel = staminaLevel;
data.staminaProg = staminaProg;
data.maxStaminaProg = maxStaminaProg;
data.stamina = stamina;
data.maxStamina = maxStamina;
data.startPos = startPos;
data.startRot = startRot;
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
if (File.Exists(Application.persistentDataPath + "/playerData.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerData.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
health = data.health;
maxHealth = data.maxHealth;
hunger = data.hunger;
maxHunger = data.maxHunger;
thirst = data.thirst;
maxThirst = data.maxThirst;
maxLevel = data.maxLevel;
playerLevel = data.playerLevel;
levelProg = data.levelProg;
maxLevelProg = data.maxLevelProg;
survivalLevel = data.survivalLevel;
survivalLevelProg = data.survivalLevelProg;
maxSurvivalLevelProg = data.maxSurvivalLevelProg;
staminaLevel = data.staminaLevel;
staminaProg = data.staminaProg;
maxStaminaProg = data.maxStaminaProg;
stamina = data.stamina;
maxStamina = data.maxStamina;
startPos = data.startPos;
startRot = data.startRot;
assignData();
}
}
public void assignData()
{
Player.GetComponent<ThirdPersonCharacter>().Health = health;
Player.GetComponent<ThirdPersonCharacter>().maxHealth = maxHealth;
Player.GetComponent<ThirdPersonCharacter>().Hunger = hunger;
Player.GetComponent<ThirdPersonCharacter>().maxHunger = maxHunger;
Player.GetComponent<ThirdPersonCharacter>().Thirst = thirst;
Player.GetComponent<ThirdPersonCharacter>().maxThirst = maxThirst;
Player.GetComponent<ThirdPersonCharacter>().maxLevel = maxLevel;
Player.GetComponent<ThirdPersonCharacter>().level = playerLevel;
Player.GetComponent<ThirdPersonCharacter>().survivalLevel = survivalLevel;
Player.GetComponent<PlayerAttack>().staminaLevel = staminaLevel;
Player.GetComponent<PlayerAttack>().staminaStats = staminaProg;
Player.GetComponent<PlayerAttack>().stamina = stamina;
Player.GetComponent<PlayerAttack>().maxStamina = maxStamina;
Player.transform.position = startPos;
Player.transform.rotation = startRot;
}
}
Player Data class
[System.Serializable]
class PlayerData
{
public float health;
public float maxHealth;
public float hunger;
public float maxHunger;
public float thirst;
public float maxThirst;
public float maxLevel;
public float playerLevel;
public float levelProg;
public float maxLevelProg;
public float survivalLevel;
public float survivalLevelProg;
public float maxSurvivalLevelProg;
public float staminaLevel;
public float staminaProg;
public float maxStaminaProg;
public float stamina;
public float maxStamina;
public Vector3 startPos;
public Quaternion startRot;
}
Answer by Menyus777 · Apr 26, 2018 at 07:28 PM
If you use binaryformatter i suggest you to use the ISerializable interface so you can save out Vector3 and Quaternions by saving the X Y Z float values(float = Single).
SerializationSurrogates are the way to go, yes. For Debugging, it can also be very helpful to save data via JSON since then you can view it in plain text form to see exatly what and how things are being saved.
Answer by Mike-Geig · Apr 26, 2018 at 06:26 PM
Vector3 and Quaternion are not serializable. If I remove those bits, your code works fine. You should be getting an error about that in your project
You can check here for a wrapper solution to serialize that data: https://answers.unity.com/questions/432326/isnt-a-quaternion-supposed-to-be-serializable.html
As a side note, you don't need that temporary data storage in your gamePrefs class. Lines ~130 to ~158 aren't needed. Instead, just take the values from your data object and apply them to your scripts. That will prevent you from storing stale data in your gameprefs class that could lead to issues later.