- Home /
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
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..
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.