- Home /
Health value isn't subtracting correctly
So in my script, it seems as if the health value doesn't get changed at all. On line 8 it prints the health value (which I set to be 100) and on line 26 it correctly prints the health with damage subtracted. The issue is that the actual health value (the one in another game object) doesn't get changed at all. I can manually change it and it's all good, but the script below wont actually change it. It just prints on line 26 the value as if it were changed.
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;
}
else 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 != "Metal" && hit.gameObject.tag != "Wood" && hit.gameObject.tag != "Stone" && hit.gameObject.tag != "Dirt") {
//}
Destroy (gameObject);
}
Can you post the declaration of Health (from this script)
Which part? The health being identified or the health being created in the other script that this script is detecting?
well I was curious about the local variable, because of exactly what @Vio$$anonymous$$yma said, for some reason I was thinking there is some way to get a reference to it, ins$$anonymous$$d of a local variable. Anywho, you'r problem's solved
Answer by VioKyma · Sep 06, 2013 at 01:55 AM
You are assigning the health value to a local variable, which you will need to assign back to the object/component you got it from.
So just make sure you assign it back after changing. (You could also just directly assign).
For example: HealthS.Central.GetComponent(CentralHealth).Health = Health;
Placed where the Debug.Log is (line 29 in your script).
Thanks! I forget the logic used in javascript because I'm so used to lua.