- Home /
Load data from file, if scene changed, fails to load location
Hey Unity people! I am in a bit of trouble here, I made a singleton, e.g. GameControl object with a script of the same name attached to it, which gathers information from other objects and saves it to a file:
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine.SceneManagement;
public class GameControl : MonoBehaviour {
public Color color;
public static GameControl control;
//PLAYER STATS
public float playerHP, Combatxp, playerPosX, playerPosY, playerPosZ;
public int sceneIndex;
//PLAYER STATS
//ENEMY STATS
public float currentHealth, myPosX, myPosY, myPosZ;
//ENEMY STATS
public bool gameSaved, gameLoaded;
public float gameSavedF = 100;
public GameObject player, enemy;
void Start(){
color = Color.white;
}
void Update(){
player = GameObject.FindGameObjectWithTag ("Player");
enemy = GameObject.FindGameObjectWithTag ("Enemy");
if (Input.GetKeyDown (KeyCode.F5)) {
Save ();
player.GetComponent<Inventory> ().SaveInventory ();
}
if (Input.GetKeyDown (KeyCode.F9)) {
Load ();
player.GetComponent<Inventory> ().LoadInventory ();
}
}
//NODORSINA KA GameControl IR SINGLETON
void Awake(){
if (control == null) {
DontDestroyOnLoad (gameObject);
control = this;
} else if (control != null) {
Destroy (gameObject);
}
}
//NODORSINA KA GameControl IR SINGLETON
public void Save(){
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");
//SEIT IERAKSTA DATUS KURUS SAGLABAT FAILA!!
PlayerData data = new PlayerData ();
data.sceneIndex = sceneIndex;
data.playerHP = playerHP;
data.Combatxp = Combatxp;
data.playerPosX = playerPosX;
data.playerPosY = playerPosY;
data.playerPosZ = playerPosZ;
data.currentHealth = currentHealth;
data.myPosX = myPosX;
data.myPosY = myPosY;
data.myPosZ = myPosZ;
//SEIT IERAKSTA DATUS KURUS SAGLABAT FAILA!!
bf.Serialize (file, data);
file.Close ();
gameSaved = true;
}
public void Load(){
if (File.Exists (Application.persistentDataPath + "/playerInfo.dat")) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize (file);
file.Close ();
//SEIT IERAKSTA DATUS KURUS IELADET NO FAILA!!
sceneIndex = data.sceneIndex;
playerHP = data.playerHP;
Combatxp = data.Combatxp;
playerPosX = data.playerPosX;
playerPosY = data.playerPosY;
playerPosZ = data.playerPosZ;
currentHealth = data.currentHealth;
myPosX = data.myPosX;
myPosY = data.myPosY;
myPosZ = data.myPosZ;
//SEIT IERAKSTA DATUS KURUS IELADET NO FAILA!!
//SEIT IERAKSTA DATUS KURUS IZSUTIT OBJEKTIEM PEC IELADESANAS NO FAILA!!
player.GetComponent<PlayerStatsCS>().sceneIndex = sceneIndex;
player.GetComponent<PlayerStatsCS> ().playerPosX = playerPosX;
player.GetComponent<PlayerStatsCS> ().playerPosY = playerPosY;
player.GetComponent<PlayerStatsCS> ().playerPosZ = playerPosZ;
player.GetComponent<PlayerStatsCS> ().playerHP = playerHP;
player.GetComponent<PlayerStatsCS> ().Combatxp = Combatxp;
if (enemy != null) {
enemy.GetComponent<EnemyScriptCS> ().myPosX = myPosX;
enemy.GetComponent<EnemyScriptCS> ().myPosY = myPosY;
enemy.GetComponent<EnemyScriptCS> ().myPosZ = myPosZ;
enemy.GetComponent<EnemyScriptCS> ().currentHealth = currentHealth;
}
//SEIT IERAKSTA DATUS KURUS IZSUTIT OBJEKTIEM PEC IELADESANAS NO FAILA!!
player.GetComponent<PlayerStatsCS> ().LoadData ();
enemy.GetComponent<EnemyScriptCS> ().LoadData ();
}
}
void OnGUI(){
if(gameSaved){
GUI.color = color;
GUI.Label (new Rect ((Screen.width/2.4f), gameSavedF, 150, 20), "Game saved sucessfully");
if (gameSavedF >= 130f) {
gameSavedF -= 3f;
if (gameSavedF < 230 && gameSavedF >= 130) {
color.a -= Time.deltaTime;
}
} else { gameSaved = false; gameSavedF = 250; color.a = 1;}
}
}
}
[Serializable]
class PlayerData{
//SEIT IERAKSTA MAINIGOS KURU VAJAG SERIALIZET SPELETAJA IERAKSTISANAI!!
public float playerHP, Combatxp, playerPosX, playerPosY, playerPosZ;
public int sceneIndex;
//SEIT IERAKSTA MAINIGOS KURU VAJAG SERIALIZET SPELETAJA IERAKSTISANAI!!
//SEIT IERAKSTA MAINIGOS KURU VAJAG SERIALIZET PRETINIEKA IERAKSTISANAI!!
public float currentHealth, myPosX, myPosY, myPosZ;
//SEIT IERAKSTA MAINIGOS KURU VAJAG SERIALIZET PRETINIEKA IERAKSTISANAI!!
}
The code fufills its function perfectly, but if lets say I saved my game (hp, and coordinates) in Scene A, and went on my way to scene B, if I load the game in Scene B, then it only loads Scene A without actually loading my save game data (hp, and coordinates), but if I press F9 e.g. quickload again it loads fine. Baiscally it works great when loading and saving happens in the same scene. I tried many solutions.
1) tried coroutines to delay scene load 2) tried to load in a loop 2-20 times 3) tried to pass data before scene was loaded, then access load function 4) - || - in a loop
Please help, I am stumped on how to fix this issue.
Thank you a lot in advance!