This question was
closed Dec 10, 2015 at 06:52 PM by
Dave-Carlile for the following reason:
Duplicate Question - http://answers.unity3d.com/questions/1110374/why-is-this-not-working-5.html
Question by
TheFish657 · Dec 10, 2015 at 06:51 PM ·
variables
Why is this not working?
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;
}
}
}
Comment