My enemies aren't reviving when I load

Hi there!

I’m not sure when this issue started to occur, but I noticed it after finishing this lecture.

If I save the game, kill an enemy and press L to load → the enemy won’t revive even though he does load back with the proper health points (as the image shows below).

It’s important to note that if I save my game, kill an enemy, stop the game completely and then pressing play again → the enemy IS standing and alive as he should be.

Attaching a screenshot:

And here is my Health script and SavingWrapper:

using UnityEngine;
using RPG.Saving;
using RPG.Stats;
using RPG.Core;

namespace RPG.Attributes
{
    public class Health : MonoBehaviour, ISaveable
    {
        [SerializeField] float healthPoints = -1f;
        bool isDead = false;

        private void Start()
        {
            if (healthPoints < 0)
            {
                healthPoints = GetComponent<BaseStats>().GetStatFromProgression(Stat.Health);
            }
        }

        public bool IsDead()
        {
            return isDead;
        }

        public void TakeDamage(GameObject instigator, float damage)
        {
            healthPoints = Mathf.Max(healthPoints - damage, 0);
            if (healthPoints == 0 && isDead == false)
            {
                Die();
                AwardExperience(instigator);
            }
        }

        public float GetPercenetage()
        {
            return 100 * (healthPoints / GetComponent<BaseStats>().GetStatFromProgression(Stat.Health));
        }

        private void Die()
        {
            if (isDead) return;
            isDead = true;
            GetComponent<Animator>().SetTrigger("die");
            GetComponent<ActionScheduler>().CancelCurrentAction();
        }

        private void AwardExperience(GameObject instigator)
        {
            Experience experience = instigator.GetComponent<Experience>();
            if (experience == null) return;

            experience.GainExperience(GetComponent<BaseStats>().GetStatFromProgression(Stat.ExperienceReward));
        }

        public object CaptureState()
        {
            return healthPoints;
        }

        public void RestoreState(object state)
        {
            healthPoints = (float)state;

            if (healthPoints <= 0)
            {
                Die();
            }
        }
    }
}

using RPG.Saving;
using System.Collections;
using UnityEngine;

namespace RPG.SceneManagement
{
    public class SavingWrapper : MonoBehaviour
    {
        const string defaultSaveFile = "save";
        [SerializeField] float fadeInTime = 2f;

        IEnumerator Start()
        {
            Fader fader = FindObjectOfType<Fader>();
            fader.FadeOutImmediate();
            yield return GetComponent<SavingSystem>().LoadLastScene(defaultSaveFile);
            yield return fader.FadeIn(fadeInTime);
        }

        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.L))
            {
                Load();
            }

            if (Input.GetKeyDown(KeyCode.S))
            {
                Save();
            }

            if (Input.GetKeyDown(KeyCode.Delete))
            {
                Delete();
            }
        }

        public void Load()
        {
            //call to the saving system load
            GetComponent<SavingSystem>().Load(defaultSaveFile);
        }

        public void Save()
        {
            GetComponent<SavingSystem>().Save(defaultSaveFile);
        }

        public void Delete()
        {
            GetComponent<SavingSystem>().Delete(defaultSaveFile);
        }
    }
}

If anymore of my code is required please let me know and I’ll paste it as soon as I can.

Thank you!

Hey @SelaMalka . The issue is related to how you “Load” your game. When you press “L” you’re doing a “soft reload” not a complete one. If I remember correctly you can set up the “L” to trigger the same full reload. Looking over my code (it’s been a while since I made the course) I see I made a “ManualLoad” function that triggers the full Load Coroutine

1 Like

@mihaivladan has the right of it. Our L)oad key doesn’t do a true reloading of the scene, as it’s meant for more shorter term testing (in fact, by the end of the course series, you’ll be removing that L key altogether in favor of a menu driven approach).

It’s far from the only issue caused by not truly reloading the scene. Simply call the ManualLoad() method shown above in the Update() method instead of Load() (we still need Load() to work “as is” or scene transitions will go into a tailspin of reloads) and all should work as intended.

Privacy & Terms