- Home /
Stats effecting the max Health Value
What I want to do here is change the MaxHealth Value each time the "Vitality Stat" Value changes. For example Maxhealth = 100 + (Vitality * 8); (This would give me an error message because I would do this.
PlayerStats playerStats;
public float MaxHealth = 100 + (playerStats.Vitality * 8);
public void Start{
ETC....
}
This would give me a weird error message. Then I tried to do. public float maxHealth;
void Start ()
{
maxHealth = (100 + (playerStats.Vitatlity * 8) / 2);
currentHealth = maxHealth;
currentMana = maxMana;
}
This wouldnt Work either... And i also tried it in in the Update method which seemed more correct but still it didnt do what i wanted it to doo!
Here are my 2 scripts that are needed for this to hopefully work.
public class PlayerHealth : MonoBehaviour {
PlayerStats playerStats;
public float currentHealth;
public float maxHealth;
public Slider playerHealthBar;
public int healthRegen = 4;
public float currentMana;
public float maxMana = 1000;
public Slider playerManaBar;
public int manaRegen = 12;
CharacterController playerMovement;
private bool isDead;
void Start ()
{
currentHealth = maxHealth;
currentMana = maxMana;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Minus))
DealDamage(6);
if (Input.GetKeyDown(KeyCode.Period))
UseMana(52);
if (currentHealth < maxHealth)
currentHealth += healthRegen * Time.deltaTime;
if (currentMana < maxMana)
currentMana += manaRegen * Time.deltaTime;
if (currentHealth <= 0)
KillPlayer();
}
public void DealDamage(int damageValue)
{
currentHealth -= damageValue;
}
public void SetMaxHealth()
{
currentHealth = maxHealth;
}
void UseMana(float manaUseValue)
{
currentMana -= manaUseValue;
if (currentMana <= 0)
currentMana = 0;
}
void KillPlayer()
{
Destroy(gameObject);
}
}
public class PlayerStats : MonoBehaviour {
public float Strength = 6;
public float Vitatlity = 10;
public float Agility = 6;
public float Intellect = 6;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Answer by Patrick2607 · May 09, 2017 at 07:10 PM
You need to instantiate the PlayerStats before you can access its properties
void Start ()
{
playerStats = new PlayerStats(); // <-- THIS
maxHealth = (100 + (playerStats.Vitatlity * 8) / 2);
currentHealth = maxHealth;
currentMana = maxMana;
}
Also, if you are only using the PlayerStats to store the values (with some functions maybe), you don't need the MonoBehaviour implementation.
public class PlayerStats
{
public float Strength = 6;
public float Vitatlity = 10;
public float Agility = 6;
public float Intellect = 6;
}
The PlayerStats class above will be enough. If you only need the properties, you can change the class to a struct.
But wouldn't it mean that if I had the maxHealth in the Start method. If the value of Vitality changes which it does after let's say a Level Up... wouldn't it mean that the health value would stay the same even if there is an increase of Vitality since it is set in the start method.
What's happening is that what you said works! but if I change the value of the Vitality the $$anonymous$$axHealth does not change with it.. how would I make it that the maxHealth would always Update when the Vitality Value changes!
You can redeclare the maxHealth value. Let's say you want to LevelUp your character. Let's create a function for that:
public class Player
{
...
public void LevelUp()
{
playerStats.Upgrade();
Recalculate$$anonymous$$axHealth();
Set$$anonymous$$axHealth(); //If you want to give the player full health after leveling up
}
private void Recalculate$$anonymous$$axHealth()
{
maxHealth = (100 + (playerStats.Vitality * 8) / 2);
}
...
}
public class PlayerStats
{
private float _upgradeRate = 1.1f;
public float Strength = 6;
public float Vitality = 10;
public float Agility = 6;
public float Intellect = 6;
public void Upgrade()
}
Strength *= _upgradeRate;
Vitality *= _upgradeRate;
Agility *= _upgradeRate;
Intellect *= _upgradeRate;
}
}
We can now call the Players' LevelUp method which upgrades(multiplies) all properties in the PlayerStats by 1.1 (you can adjust it how you want it, this is just an example). Then it recalculates the $$anonymous$$axHealth.
Just wanna Thanks, alot! Your da Best!
$$anonymous$$aybe we will see each other in another of my questions ;) I am looking forward to it ;D!
THAN$$anonymous$$S!
Answer by adamwolanski · May 10, 2017 at 01:07 PM
@NightmareTD There is no need to inherit MonoBehaviour in PlayerStats. In this case playerStats is null and you need to instantiate PlayerStats component. Alternatively you can try this:
public class PlayerStats {
public float Strength;
public float Vitality;
public float Agility;
public float Intellect;
public PlayerStats()
{
Strength = 6;
Vitality = 10;
Agility = 6;
Intellect = 6;
}
}
void Start ()
{
playerStats = new PlayerStats();
...
}
To make sure please copy error message you received.