Awake() vs Start()

In this lecture, the
if (healthPoints < 0)
fixed one potential bug, but introduced another bug where enemies’ healthpoints of different character class will not be updated according to what we set in progression.

Why not cache or initialize it in the Awake()? It will solve the problem?

There’s definitely some racing in some of the scripts, but that will be eventually sorted out, if you want to fix it on your own then that’s great, just keep in mind that Sam will cover that later on.

Hello, Whenever I save my game and play again the health is always full rather than what it should be from previous health. For Example; My player has a health of 60% in a current game and if i have to gain play the health is full. How to solve this ?

here is the code from the lecture , I even don’t know where is the error,

using UnityEngine;

using RPG.Saving;

using RPG.Stats;

using RPG.Core;

using System;

namespace RPG.Resources

{

public class Health : MonoBehaviour, ISaveable

{

    float healthPoints = -1f;

    bool isDead = false;

   private void Awake()

   {

       if (healthPoints < 0)

       {

            healthPoints = GetComponent<BaseStats>().GetStat(Stat.Health);

       }

       

   }

    public bool IsDead()

    {

        return isDead;

    }

   

    public void TakeDamage(GameObject instigator, float damage)

    {

        healthPoints = Mathf.Max(healthPoints - damage, 0);

        if(healthPoints == 0)

        {

            Die();

            AwardExperience(instigator);

        }

    }

    public float GetPercentage()

    {

        return 100 * (healthPoints / GetComponent<BaseStats>().GetStat(Stat.Health));

    }

    public float GetHealthPoints()

    {

        return healthPoints; //Or whatever you named your varable for current heatlh points.

    }

   

    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>().GetStat(Stat.ExperienceReward));

    }

   

    public object CaptureState()

    {

        return healthPoints;

    }

    public void RestoreState(object state)

    {

        healthPoints = (float) state;

        

        if (healthPoints <= 0)

        {

            Die();

        }

    }

}

}

I’m not that good, but your health script looks ok…sounds like you have a problem in RestoreState or LoadLastScene or unlikely HealthDisplay (just crosscheck health from inspector or debug.log). Did you build the SavingSystem script yourself? If yes, check the LoadLastScene function. Else, make sure you have SavingSystem and SavingWrapper scripts hooked up nicely. Or, are you at the lecture where the script execution order was messed around? if yes make sure you arrange it back. If with all these, your problem still persist, just push on, like Yee said, it will probably get fixed by Sam during the Race lecture. Good Luck.

I didn’t build my own saving system i think thats why,

public object CaptureState()

{

    return healthPoints;

}

public void RestoreState(object state)

{

    healthPoints = (float) state;

    

    if (healthPoints <= 0)

    {

        Die();

    }

}

}


}

Bla_Ck,
Is it your UI that gives you the impression that the health isn’t being set correctly? Paste in your HealthDisplay script and we’ll take a look at what may be going on there.

This topic was automatically closed after 36 hours. New replies are no longer allowed.

Privacy & Terms