When I save It saves the default health instead of the current health for enemies and the player for the saving asset pack module. Based on the debug info I added it looks like it is saving twice. Once with the correct health then once with the default health so It ends up keeping the default health. Can someone help me?
Post your Health.cs script here, and we’ll have a look. Don’t forget to format it with the </> code button.
Sorry I didnt earlier.
using System.Collections;
using UnityEngine;
using RPG.Saving;
namespace RPG.Core {
public class Health : MonoBehaviour, ISaveable {
[SerializeField] float health = 100f;
bool isDead = false;
public void TakeDamage(float damage) {
health -= damage;
// print("health is " + health);
if(health <= 0) {
Die();
}
}
public bool IsDead() {
return isDead;
}
private void Die() {
if (!isDead) {
GetComponent<Animator>().SetTrigger("die");
isDead = true;
GetComponent<ActionScheduler>().CancelCurrentAction();
}
}
public object CaptureState() {
if (gameObject.CompareTag("Player")){
print($"{name} saved health as {health} in scene{UnityEngine.SceneManagement.SceneManager.GetActiveScene().buildIndex}");
}
return health;
}
public void RestoreState(object state) {
if (gameObject.CompareTag("Player")) {
print($"{name} set health to {state}");
}
health = (float)state;
if (health == 0) {
Die();
}
}
}
}
In addition here is my portal and Saving Wrapper classes
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.AI;
namespace RPG.SceneManagement {
public class Portal : MonoBehaviour {
enum DestinationIdentifier {
A,B,C,D,E
}
[SerializeField] int sceneToLoad = -1;
[SerializeField] Transform spawnPoint;
[SerializeField] DestinationIdentifier destination;
[SerializeField] float fadeOutTime = 1;
[SerializeField] float fadeInTime = 2;
[SerializeField] float fadeWaitTime = 0.5f;
private void OnTriggerEnter(Collider other) {
if(other.CompareTag("Player")) {
StartCoroutine(Transition());
}
}
private IEnumerator Transition() {
if (sceneToLoad < 0) {
Debug.LogError("Scene to load not set");
yield break;
}
DontDestroyOnLoad(gameObject);
Fader fader = FindObjectOfType<Fader>();
yield return fader.FadeOut(fadeOutTime);
SavingWrapper wrapper = FindObjectOfType<SavingWrapper>();
print("save with portal");
wrapper.Save();
yield return SceneManager.LoadSceneAsync(sceneToLoad);
wrapper.Load();
Portal otherPortal = GetOtherPortal();
UpdatePlayer(otherPortal);
yield return new WaitForSeconds(fadeWaitTime);
yield return fader.FadeIn(fadeInTime);
Destroy(gameObject);
}
private void UpdatePlayer(Portal otherPortal) {
GameObject player = GameObject.FindWithTag("Player");
player.GetComponent<NavMeshAgent>().enabled = false;
player.transform.rotation = otherPortal.spawnPoint.rotation;
player.transform.position = otherPortal.spawnPoint.position;
player.GetComponent<NavMeshAgent>().enabled = true;
}
private Portal GetOtherPortal() {
foreach(Portal portal in FindObjectsOfType<Portal>()) {
if (portal == this) continue;
if (portal.destination != destination) continue;
return portal;
}
throw new Exception("no portal in this scene");
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Saving;
namespace RPG.SceneManagement {
public class SavingWrapper : MonoBehaviour {
const string DEFAULT_SAVE_FILE = "save";
void Update() {
if (Input.GetKeyDown(KeyCode.L)) {
Load();
}
if (Input.GetKeyDown(KeyCode.S)) {
print("saved with command");
Save();
}
}
public void Save() {
GetComponent<SavingSystem>().Save(DEFAULT_SAVE_FILE);
}
public void Load() {
GetComponent<SavingSystem>().Load(DEFAULT_SAVE_FILE);
}
}
}
Based on the code provided, I’m puzzled that it’s saving twice… (further along the line, it should be, as we want to merge all of the save data into one file, so we save, load, then save again, but there’s not a second save command in Portal)
I’d like to take a closer look. Zip up your project and upload it to https://gdev.tv/projectupload and I’ll see if I can suss out what’s going on here.
it isnt letting me go to that website it just says site can not be reached
That’s because I messed up when I typed out the address. Sorry about that. https://gdev.tv/projectupload
I found the issue. Both the player and enemy had two health components on them. The first health component was absorbing the damage, both health components where saving, with the 2nd health component being the one to be saved (as it’s second, and overwrote the first). On restoring, it restores the untouched health value. Open each of the prefabs and remove the last health component on each.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.