Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by BIO_K · Apr 26, 2018 at 06:23 PM · script.sceneload

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;
     }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Apr 26, 2018 at 08:54 PM 1
Share

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.

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

101 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

async loading scene 0 Answers

Load Scene from intermediary Scene 1 Answer

how to keep the script working after scene load? 1 Answer

HELP Game Scene 8 (-1) Cannot be loaded because it has not been added to the build settings 1 Answer

Input.GetMouseButton on multiple objects 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges