I have a simple piece of code to decrease an object’s “life” when I press the x button. When I play the game and press x, the life goes down random amounts and does so until it is deleted. Please can you help me find a solution to this or even tell me why this is happening and what I have done wrong.
Thank you in advance
This is the code which I am using:
public int life = 10;
void Start () {
}
void Update () {
if (life < 1)
{
Debug.Log ("You Win");
Destroy (gameObject, 1f);
}
if (Input.GetKey ("x"))
{
life = life - 1;
}
}
}