- Home /
Help: My variable is always resetting to default value during play mode.
I have a simple script to keep track of a public HP variable and it also has a function Apply1Damage() that reduces HP by 1 everytime it is called. I also have a script which SendMessage() when a collision or a trigger is detected (OnTriggerEnter, OnCollisionEnter). I have applied the HP script to my enemies object and it works perfectly. However, when I apply the same script to my player object, the HP variable keep resetting to the initial value at a very fast speed. Using Debug.Log, I see that my player is receiving the damage and that the health variable is being reduced, but never less than the initial value-1. What could be causing this?
Here are the relevant scripts:
public class DamageableMonster : MonoBehaviour {
public float health;
Animator damageableObjectAnimator;
void Start()
{
damageableObjectAnimator = GetComponent<Animator>();
}
void apply1Damage()
{
health--;
Debug.Log("Health down to: " + health);
}
void Update () {
if (health <= 0)
{
damageableObjectAnimator.SetTrigger("Death");
}
}
}
And
public class Apply1Damage : MonoBehaviour {
public float knockback;
// Use this for initialization
void Start () {
knockback = 1000;
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.layer != 8)
{
Debug.Log("HIT");
float xDistance = (other.transform.position.x - gameObject.GetComponentInParent<Transform>().position.x);
float yDistance = (other.transform.position.y - gameObject.GetComponentInParent<Transform>().position.y);
other.collider.SendMessage("apply1Damage");
other.rigidbody.AddForce(new Vector2(xDistance * knockback, yDistance * knockback));
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.layer != 8)
{
Debug.Log("HIT");
float xDistance = (other.transform.position.x - gameObject.GetComponentInParent<Transform>().position.x);
float yDistance = (other.transform.position.y - gameObject.GetComponentInParent<Transform>().position.y);
other.SendMessage("apply1Damage");
other.GetComponent<Rigidbody2D>().AddForce(new Vector2(xDistance * knockback, yDistance * knockback));
}
}
}
What could be causing this?
About a million things. Show us your code and we might be able to narrow it down to a 10 ;)
What Nose said - it sounds as though the health is being reset in Update() though?
Learn how to use debugger and breakpoints. It will help you very much
I edited my original post and added the 2 scripts. As I said, it work fine with ennemies being hit, but not with the player being hit, for some reason..
What about the layers? Is the player in layer 8? Does the log print the "Health down to: ..."?
Answer by Coffee-with-Venky · Jul 03, 2015 at 07:29 AM
if you are trying to decrease health when gameobject,layer 8 ...........!!! then use like this
public class Apply1Damage : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.layer ==8)
{
// if gameobject triggered with layer 8 it will work
other.SendMessage("apply1Damage");
}
}
//finds with name !!!
void OnTriggerEnter2D(Collider2D other) { if (other.gameObject.name=="Enemy") { other.SendMessage("apply1Damage"); } }
}
Answer by KevinCodes4Food · Jul 02, 2015 at 07:12 PM
Check your scene to see if any object is colliding with your player.
Change
Debug.Log("HIT");
to
Debug.Log(name + " was HIT by " + other.name);
To find out what is colliding with your player. If may be the ground, an attached equipment item, etc.