- Home /
Slider calculations go wrong
I have a script attached to a box that makes the player lose 20% of his maximum health on touch. This is the script:
public Slider health;
void OnCollisionEnter(Collision col){
if(col.gameObject.tag=="Player"){
health.value=health.value-0.2f;
Debug.Log ("Hit "+health.value);
}
}
After that I started checking this script and moved player the way he could hit the harm-box multiple times before he dies. And then goes the weird part. Each time player needs 7 collisions to become fully dead and this is the result list of the health slider value after each hit:
0.8
0.6
0.4
0.2
2.980232E-08
0.1
0.0
Could anyone please explain me why is this happening?
Answer by chadli · Feb 17, 2015 at 09:11 PM
Is there a reason that this value needs to be a float? can it not be represented by an int or something more simple?
I would assume the error does not lie within the code that you have posted and may lie in the health class.
Try it with an int instead of float. Float may be easier conceptually to think about, but there are nitty gritty things that come with that as well. If there is no specific reason to use float - use int.
It worked, thank you! Started using int value from 0 to 100 and each hit decreases health by 20.