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 Malwallace12 · Apr 27, 2016 at 05:22 AM · positionserializationsaveload

Character Positions Not Serializing or Loading Properly?

I'm using a custom saving and loading script that is supposed to store player names, positions, stats, etc. It has no problem with saving the names and list of character class items, but for some reason it won't store or load the positions. My characters always load at the default position. Any idea what could be the culprit? Save Load Script public class SaveLoad { public static GameClass savedGame;

     public static void Save()
     {   
         
         savedGame = GameClass.current;
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create(Application.persistentDataPath + "/saveData.sd");
         bf.Serialize(file, SaveLoad.savedGame);
         file.Close();
     }
 
     public static void Load()
     {
         if (File.Exists(Application.persistentDataPath + "/saveData.sd"))
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/saveData.sd", FileMode.Open);
             SaveLoad.savedGame = (GameClass)bf.Deserialize(file);
             file.Close();
             GameClass.current = savedGame;
         }
     }
 }

Character Class Script using UnityEngine; using System.Collections; using UMA;

 [System.Serializable]
 public class Character{
     public string name;
     public int health;
     public string condition;
     public int armStrength;
     public int legStrength;
     public string lifeStage;
     public int relationshipScore;
     public float xPosition;
     public float yPosition;
     public float zPosition;
 
     public Character()
     {
         this.name = "";
         this.health = 0;
         this.condition = "";
         this.armStrength = 0;
         this.legStrength = 0;
         this.lifeStage = "";
         this.relationshipScore = 0;
         this.xPosition = 1789.0f;
         this.yPosition = 282.0f;
         this.zPosition = 443.0f;
     }
 }

Game Class

using UnityEngine; using System.Collections; using System.Collections.Generic;

[System.Serializable] public class GameClass{ public static GameClass current = new GameClass(); public List characterList = new List(); public int curCharIndex; } Game Controller Script using UnityEngine; using System.Collections; using UMA;

public class GameControl : MonoBehaviour {

 public GameObject Player;
 public Transform characterPrefab;
 public int curCharIndex;
 public bool hasLoaded = false;
 public UMATextRecipe recipe;
 
 public GameObject[] selectorArr;
 public GameClass game;
 // Use this for initialization

 void Awake(){
     DontDestroyOnLoad (GameObject.Find ("GameControl"));

 }
 void Start () {
    
 }
 
 // Update is called once per frame
 void Update () {
     game = GameClass.current;
   if (Application.loadedLevel == 2)
     {
         if (!hasLoaded)
         {

             
                 SaveLoad.Load();
                 curCharIndex = GameClass.current.curCharIndex;


             selectorArr = new GameObject[GameClass.current.characterList.Count];
             for ( int x = 0; x < GameClass.current.characterList.Count; x++)
             {
                 Vector3 loadposition = new Vector3(GameClass.current.characterList[x].xPosition, GameClass.current.characterList[x].yPosition, GameClass.current.characterList[x].zPosition);
                 GameObject go = Instantiate(Player, loadposition, Quaternion.identity) as GameObject;
                 var playerAvatar = go.GetComponent<UMADynamicAvatar>();
                 playerAvatar.umaGenerator = GameObject.Find("UMAGenerator").GetComponent<UMAGenerator>();
                 go.name = GameClass.current.characterList[x].name;
                 
                 
                     go.AddComponent<Locomotion>();

                 var rigidbody = go.AddComponent<Rigidbody>();
                 rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
                 var CharCollider = go.AddComponent<CapsuleCollider>();
                 CharCollider.height = 2;
                 CharCollider.radius = .25f;
                 CharCollider.center = new Vector3(0,.75f,0);
                 var path = "Assets/_Character Saves/" + go.name + ".txt";
                 var asset = ScriptableObject.CreateInstance<UMATextRecipe>();
                 asset.recipeString = FileUtils.ReadAllText(path);
                 playerAvatar.Load(asset);
                 recipe = asset;
                 Destroy(asset);
                 selectorArr[x] = go;
                
             }

             hasLoaded = true;

         }
    
      
             for (var x = 0; x < GameClass.current.characterList.Count; x++)
             {
                 if (selectorArr[x].name == GameClass.current.characterList[x].name)
                 {
                     GameClass.current.characterList[x].xPosition = selectorArr[x].transform.position.x;
                     GameClass.current.characterList[x].yPosition = selectorArr[x].transform.position.y;
                     GameClass.current.characterList[x].zPosition = selectorArr[x].transform.position.z;
                 }
             }
        

     }
 }

}

It stores the updated positions properly into GameClass.current.CharacterList[] during run time, but when I go to load the saved data file, it loads them at the default position. Please let me know if you see something that I need to change.

Thank you

Comment
Add comment · Show 1
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 Malwallace12 · Apr 27, 2016 at 01:09 AM 0
Share

Update: I tried testing a health value. I set the default health for new characters to 100 and set up a button to reduce health by 10. $$anonymous$$ost of the time, when I click the button, nothing happens. Every several clicks, it reduces the health to 90 but is reset to 100 on the next click or button press. I really have no idea what is going on. I've checked through my scripts meticulously and I still can't find the culprit. Hopefully one of you can see the issue.. Thanks again.

Also, when I added the save function to the damage button, it would register the changes to the health properly and save and load the change to the health, but still nothing for the location. I'm really at a loss here..

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Malwallace12 · Apr 29, 2016 at 06:26 AM

Okay, I figured it out. I had written in a Load function in the Update function of my Locomotion animation script in order to tell the program which avatar was being controlled, in case I switched the current character index to another avatar. I should have put it in a Start function to begin with, but in any case, I don't think it will actually be necessary to include it. I didn't realize how persistent the GameClass data was across my scripts. Sorry for wasting time, hopefully this might help someone else in the future.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can playerprefs save my tower model and its position? 1 Answer

Unity Not Loading 0 Answers

Unity Serializer - saves delete after restarting game 2 Answers

How to save and load the game of the corresponding player by Serialization 1 Answer

Save/Load mutiple game objects as one file 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