Save/Load Player System
Hello! So I tried this script from a guy hopeing that would work. But my problems are:
1- Doesn't give any errors. 2- I don't know if saves. 3- It doens't load.
SaveSystem Script
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem
{
public static void SavePlayer (Player player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.save";
FileStream stream = new FileStream(path, FileMode.Create);
PlayerData data = new PlayerData(player);
formatter.Serialize(stream, data);
stream.Close();
}
public static PlayerData LoadPlayer ()
{
string path = Application.persistentDataPath + "/player.save";
if(File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
PlayerData data = formatter.Deserialize(stream) as PlayerData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save nao encontrado" + path);
return null;
}
}
}
PlayerData Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class PlayerData
{
//public float health;
//public float hunger;
//public float thirst;
public float[] position;
public PlayerData(Player player)
{
//health = player.health;
//hunger = player.hunger;
//thirst = player.thirst;
position = new float[3];
position[0] = player.transform.position.x;
position[1] = player.transform.position.y;
position[2] = player.transform.position.z;
}
}
Player Script(Used for the buttons)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Diagnostics;
[System.Serializable]
public class Player : MonoBehaviour
{
[SerializeField] private Transform player;
[SerializeField] private Transform respawnPoint;
public float maxHealth, maxThirst, maxHunger;
public float thirstIncreaseRate, hungerIncreaseRate;
public float health;
public bool dead;
public float damage;
public bool weaponEquipped;
public static bool triggeringWithAI;
public static GameObject triggeringAI;
public void Start()
{
health = maxHealth;
}
public void Update()
{
//Detetar e matar AI
if (triggeringWithAI == true && triggeringAI)
{
if(Input.GetMouseButtonDown(0))
{
Attack(triggeringAI);
}
}
if (!triggeringAI)
triggeringWithAI = false;
}
//Ataque
public void Attack(GameObject target)
{
if (target.tag == "Animal")// && weaponEquipped == true
{
Animal animal = target.GetComponent<Animal>();
animal.health -= damage;
}
}
//Player morre e renasce
public void Die()
{
dead = true;
print("Morreste...");
Respawn();
}
public void Respawn()
{
dead = false;
GetComponent<PlayerVitals>().healthSlider.value = maxHealth;
GetComponent<PlayerVitals>().hungerSlider.value = maxHunger;
GetComponent<PlayerVitals>().thirstSlider.value = maxThirst;
player.transform.position = respawnPoint.transform.position;
}
//Beber
public void Drink(float decreaseRate)
{
GetComponent<PlayerVitals>().thirstSlider.value += decreaseRate;
}
//Comer
public void Eat(float decreaseRate)
{
GetComponent<PlayerVitals>().hungerSlider.value += decreaseRate;
}
//Curar
public void Heal(float decreaseRate)
{
GetComponent<PlayerVitals>().healthSlider.value += decreaseRate;
}
//Ao colidir com Animal
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Animal")
{
triggeringAI = other.gameObject;
triggeringWithAI = true;
}
}
public void OnTriggerExit(Collider other)
{
if (other.tag == "Animal")
{
triggeringAI = null;
triggeringWithAI = false;
}
}
//Save-Load
public void SavePlayer()
{
SaveSystem.SavePlayer(this);
}
public void LoadPlayer()
{
PlayerData data = SaveSystem.LoadPlayer();
//health = data.health;
//hunger = data.hunger;
//thirst = data.thirst;
Vector3 position;
position.x = data.position[0];
position.y = data.position[1];
position.z = data.position[2];
transform.position = position;
}
}
I hope someone can help me.
Best regards.
,Hello! So I made this Script to make a Save/Load system. At the moment doesn't give errors, but doesn't load(I don't know if saves to). I hope someone could help!
Save System
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem
{
public static void SavePlayer (Player player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.save";
FileStream stream = new FileStream(path, FileMode.Create);
PlayerData data = new PlayerData(player);
formatter.Serialize(stream, data);
stream.Close();
}
public static PlayerData LoadPlayer ()
{
string path = Application.persistentDataPath + "/player.save";
if(File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
PlayerData data = formatter.Deserialize(stream) as PlayerData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save nao encontrado" + path);
return null;
}
}
}
Player Data
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class PlayerData
{
public float health;
//public float hunger;
//public float thirst;
public float[] position;
public PlayerData(Player player)
{
health = player.health;
//hunger = player.hunger;
//thirst = player.thirst;
position = new float[3];
position[0] = player.transform.position.x;
position[1] = player.transform.position.y;
position[2] = player.transform.position.z;
}
}
Player Script to make load and save from a button
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Diagnostics;
public class Player : MonoBehaviour
{
[SerializeField] private Transform player;
[SerializeField] private Transform respawnPoint;
public float maxHealth, maxThirst, maxHunger;
public float thirstIncreaseRate, hungerIncreaseRate;
public float health;
public bool dead;
public float damage;
public bool weaponEquipped;
public static bool triggeringWithAI;
public static GameObject triggeringAI;
public void Start()
{
health = maxHealth;
}
public void Update()
{
//Detetar e matar AI
if (triggeringWithAI == true && triggeringAI)
{
if(Input.GetMouseButtonDown(0))
{
Attack(triggeringAI);
}
}
if (!triggeringAI)
triggeringWithAI = false;
}
//Ataque
public void Attack(GameObject target)
{
if (target.tag == "Animal")// && weaponEquipped == true
{
Animal animal = target.GetComponent<Animal>();
animal.health -= damage;
}
}
//Player morre e renasce
public void Die()
{
dead = true;
print("Morreste...");
Respawn();
}
public void Respawn()
{
dead = false;
GetComponent<PlayerVitals>().healthSlider.value = maxHealth;
GetComponent<PlayerVitals>().hungerSlider.value = maxHunger;
GetComponent<PlayerVitals>().thirstSlider.value = maxThirst;
player.transform.position = respawnPoint.transform.position;
}
//Beber
public void Drink(float decreaseRate)
{
GetComponent<PlayerVitals>().thirstSlider.value += decreaseRate;
}
//Comer
public void Eat(float decreaseRate)
{
GetComponent<PlayerVitals>().hungerSlider.value += decreaseRate;
}
//Curar
public void Heal(float decreaseRate)
{
GetComponent<PlayerVitals>().healthSlider.value += decreaseRate;
}
//Ao colidir com Animal
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Animal")
{
triggeringAI = other.gameObject;
triggeringWithAI = true;
}
}
public void OnTriggerExit(Collider other)
{
if (other.tag == "Animal")
{
triggeringAI = null;
triggeringWithAI = false;
}
}
//Save-Load
public void SavePlayer()
{
SaveSystem.SavePlayer(this);
}
public void LoadPlayer()
{
PlayerData data = SaveSystem.LoadPlayer();
//health = data.health;
//hunger = data.hunger;
//thirst = data.thirst;
Vector3 position;
position.x = data.position[0];
position.y = data.position[1];
position.z = data.position[2];
transform.position = position;
}
}
Best Regards
Your answer
Follow this Question
Related Questions
PlayerPrefs saving and Loading 2 Answers
Player not moving in the right direction instantly 1 Answer
How do i ajust the player height acording to the height of the roof in a first person game? 1 Answer
Help offline progression, works on windows but not on Android. 0 Answers
There is a problem with my script... 0 Answers