Hello, I’m working on a small game - it’s my first, and I’m having problem with this.
I’m trying to use properties in a class as a storage for how many HP etc. a player base has. However when I change the property from one script, it doesn’t affect any other the values of other scripts that use the same property.
This is the class with the properties:
public class BaseStats {
public int baseHealth = 1000;
public int BaseHealth
{
get
{
return baseHealth;
}
set
{
baseHealth = value;
}
}
This is a script on an enemy that shoud decrease the value on collision
public class OnHit : MonoBehaviour {
BaseStats baseStats = new BaseStats();
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Base")
{
baseStats.BaseHealth -= 100;
Destroy(gameObject);
}
}
}
And this is a script on a Text object that should display current HP
public class ShowHealth : MonoBehaviour {
public BaseStats baseStats = new BaseStats();
Text txt;
void Start ()
{
txt = GetComponent<Text>();
}
void Update ()
{
txt.text = baseStats.BaseHealth.ToString();
}
}
It displays the default value, which is 1000, but it doesn’t change after the enemy collides with base.
What am I doing wrong?