Health isn't being saved and restored correctly

I have tried many different approaches to fixing the issue with enemies sometimes coming back to life, it will work for a bit and then suddenly the enemies are coming back to life again. I’m now even noticing the player’s health is being restored to 100% even if the player has taken damage.

This only happens when either the portals or leaving and reentering playmode, so I assume it must be something to do with how health isbeing saved because everything else such as weapons and experience are being saved and restored correctly.

I’ve set the enemies prefab health to -1 and in the start of Health.cs:

private void Start()
        {
            if (_healthPoints <= 0)
            {
                _healthPoints = _baseStats.GetStat(Stat.Health);
            }
        }
     public object CaptureState()
        {
            return _healthPoints;
        }

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

            if (_healthPoints <= 0)
            {
                PutInDeadState();
            }
        }

Note: I’ve even tried commenting out the start method and the dead enemies are still being restored with full health at times.

Before sharing my code, its worth noting I have just changed it slightly to try the suggestion from:

Which apparently makes it so everything will only be restored after the scene has completed loading.

In Portal.cs:

 IEnumerator Transition()
        {
            if (_sceneToLoad < 0)
            {
                Debug.LogError("Scene to load not set");
                yield break;
            }
            SavingWrapper savingWrapper = FindObjectOfType<SavingWrapper>();

            Object.DontDestroyOnLoad(gameObject);
            savingWrapper.Save();

            yield return _fader.FadeOut(_fadeOutTime);
            AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(_sceneToLoad);
            while (asyncLoad.isDone == false)
            {
                yield return null;
            }
            savingWrapper.Load();

            Portal otherPortal = GetOtherPortal();
            UpdatePlayer(otherPortal);

            savingWrapper.Save();

            yield return new WaitForSeconds(_fadeWaitTime);
            yield return _fader.FadeIn(_fadeInTime);

            Destroy(gameObject);
        }

In SavingSystem.cs:

public void Save(string saveFile)
        {
            Dictionary<string, object> state = LoadFile(saveFile);
            CaptureAllStates(state);
            SaveFile(saveFile, state);
        }

    private void CaptureAllStates(Dictionary<string, object> state)
        {
            foreach (SaveableEntity saveable in FindObjectsOfType<SaveableEntity>())
            {
                state[saveable.GetUniqueIdentifier()] = saveable.CaptureSaveableStates();
            }

            state[lastSceneBuildIndex] = SceneManager.GetActiveScene().buildIndex;
        }

I fixed it by changing the if check in start to check if hp less than 0 rather than less or equal to:

 private void Start()
        {
            if (_healthPoints < 0)
            {
                Debug.Log($"Start called for restoring health for: {gameObject.name}");
                _healthPoints = _baseStats.GetStat(Stat.Health);
            }
        }

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms