- Home /
This question was
closed Jan 02, 2017 at 08:27 PM by
Asellio for the following reason:
wasnt anwsered
Can only Save in on scene
I am only able to save in one scene and when i try to save in any other scene it either doesnt work or teleports me off the map. public class GameControl : MonoBehaviour {
public static GameControl control;
public Transform player;
private float x;
private float y;
private PlayerStats thePS;
private PlayerHealthManager pHM;
private string newUserName;
void Awake () {
if(control == null)
{
DontDestroyOnLoad(gameObject);
control = this;
}else if(control != this)
{
Destroy(gameObject);
}
}
private void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
thePS = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerStats>();
pHM = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHealthManager>();
}
public void Save()
{
PlayerPrefs.SetInt("Level", SceneManager.GetActiveScene().buildIndex);
PlayerPrefs.SetFloat("PlayerX", player.transform.position.x);
PlayerPrefs.SetFloat("PlayerY", player.transform.position.y);
Debug.Log("GameSaved");
print(player.transform.position.x);
print(player.transform.position.y);
if (!Directory.Exists("Saves"))
Directory.CreateDirectory("Saves");
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
data.currentHealth = pHM.playerCurrentHealth;
data.maxHealth = pHM.playerMaxHealth;
data.level = thePS.currentLevel;
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
SceneManager.LoadScene(PlayerPrefs.GetInt("Level"));
player.transform.position = new Vector2(PlayerPrefs.GetFloat("PlayerX"), PlayerPrefs.GetFloat("PlayerY"));
print(player.transform.position.x);
print(player.transform.position.y);
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);
pHM.playerCurrentHealth = data.currentHealth;
pHM.playerMaxHealth = data.maxHealth;
thePS.currentLevel = data.level;
file.Close();
}
}
}
[Serializable]
class PlayerData
{
public int currentHealth;
public int maxHealth;
public int level;
}
Comment
Answer by Socapex · Dec 28, 2016 at 06:46 PM
Are the other scenes turned on in the build settings? You can try saving the scene name instead, and use the SceneManager.LoadScene
overload that takes a string parameter.
also how would i save the scene to then load it would it be Scene$$anonymous$$anager.GetActiveScene?