Changing property value
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?
Answer by Statement · Oct 30, 2015 at 01:45 PM
They both have a unique instance of BaseStats each.
ShowHealth.baseStats references one instance of BaseStats
OnHit.baseStats references another instance of BaseStats
Both are assigned a new BaseStats.
You want them both to reference the same instance of BaseStats if you want to share the data. Anyway, this has nothing at all to do with properties. Either get the BaseStats from the other script or create a component that both use instead.
Thanks! I thought that it maybe creates separate instances, but I wasn't quite sure. But I'm not really sure how to make them share. $$anonymous$$y other ideas were to make a script that would create an instance of BaseStats and then try to refence that script in other scripts that change or require the values.
I'm not really sure how to make them share. $$anonymous$$y other ideas were to make a script that would create an instance of BaseStats and then try to refence that script in other scripts that change or require the values.
1: You'd have to decide who "owns" the BaseStats. It doesn't sound sensible that either of them own it.
2: That's how you share the reference. Or make BaseStats that script.
Either get the BaseStats from the other script or create a component that both use ins$$anonymous$$d.
That's what I suggested. Scripts are components. Perhaps I should have said script :) Create a script that both ShowHealth and OnHit accesses. Consider promoting BaseStats to that actual script.
public class BaseStats : $$anonymous$$onoBehaviour
Then slap it on the/any game object.
The other scripts declare a public variable like:
public BaseStats baseStats;
And you can set it via the inspector, or automatically through GetComponent etc.
Yeah, I dumped that script and made a new one that I put on an empty object, and acces that ins$$anonymous$$d.
public class ValuesGetSet : $$anonymous$$onoBehaviour {
public int HP,money;
void Start ()
{
HP = 1000;
money = 500;
}
public int GetHP ()
{
return HP;
}
public void SetHP(int newHP)
{
HP = newHP;
}
public int Get$$anonymous$$oney()
{
return money;
}
public void Set$$anonymous$$oney(int new$$anonymous$$oney)
{
money = new$$anonymous$$oney;
}
}
Thanks for your help! :)