- Home /
The question is answered, right answer was accepted
"Health" value doesn't change?
This is a piece of my script. The health value equals what I tell it to, but each time it calculates it as if the Health value doesn't change at all. Anything look weird in this:
if(other.collider.GetComponent(HealthSystem)) {
HealthS = other.collider.GetComponent(HealthSystem);
Health = HealthS.Central.GetComponent(CentralHealth).Health;
if(HealthS.IsHead == true) {
Debug.Log("Hit something!");
Health = Health - Damage;
}
if(HealthS.IsTorso == true) {
Debug.Log("Hit something!");
Health = Health - Damage / 2;
}
Answer by clunk47 · Aug 15, 2013 at 12:43 AM
Instead of Health = Health - Damage, try using
Health -= Damage;
With little to go off of though, not knowing what your other scripts hold, an easy way to do this would be:
other.collider.gameObject.GetComponent(HealthSystem).Health -= Damage;
Still not working. It's taking away damage, but it's like I'm saying health = 100 - damage every time. It's changing the health back to 100 every time.
If I manually change it, the value stays what I change it to. If I shoot it, it calculates the value but never actually gives it the value. It's weird. (I have a debug that states the value)
Post the full script. health = 100 - damage is obviously always going to be 100 $$anonymous$$us damage the way you have it lol. Like what I said, use health -= damage!
I think there error is in another place then. Perhaps in HealthSystem or CentralHealth
var particle : GameObject;
var life : int = 15;
var HealthS;
var Health;
var Damage : int = 50; //This is the max damage.
function Start () {
life = life * 100;
}
function Update () {
life -= Time.deltaTime * 100;
if(life == 0) {
Destroy (gameObject);
}
}
function OnCollisionEnter(other : Collision) {
if(other.collider.GetComponent(HealthSystem)) {
Debug.Log("This1");
HealthS = other.collider.GetComponent(HealthSystem);
Debug.Log("This2");
Health = HealthS.Central.GetComponent(CentralHealth).Health;
Debug.Log("This3");
Debug.Log(Health);
if(HealthS.IsHead == true) {
Debug.Log("Hit something!");
Health -= Damage;
}
if(HealthS.IsTorso == true) {
Debug.Log("Hit something!");
Health -= Damage / 2;
}
if(HealthS.IsArm == true) {
Health -= Damage / 3;
}
if(HealthS.IsLeg == true) {
Health -= Damage / 2.5;
}
if(HealthS.IsOther == true) {
Health -= Damage / 2;
}
Debug.Log(Health);
}
else {
Debug.Log(other.collider.name + " Does not have HealthSystem script attached! ");
}
//if(hit.gameObject.tag != "$$anonymous$$etal" && hit.gameObject.tag != "Wood" && hit.gameObject.tag != "Stone" && hit.gameObject.tag != "Dirt") {
//}
Destroy (gameObject);
}