NaN error when trying to adjust Scale as a percentage.
All i'm trying to do is adjust the scale of my object to the percentage of player health remaining everything works the way it's supposed to at run time, however I get an error on every update that reads:
transform.localScale assign attempt for 'HealthTexture' is not valid. Input localScale is { NaN, 1.000000, 1.000000 }.
void Update () {
healthPercentage = playerCurrentHealth / playerMaxHealth;
transform.localScale = new Vector3(healthPercentage,1,1);
}
Answer by KenJammin3 · Nov 15, 2015 at 07:08 PM
So if anyone does stumble upon this, I ended up just checking to see if my float was returning NaN and then setting it to 0 if it was to keep the variable from ending up undefined. I'm still not sure why even if the number is valid why I was still getting a NaN error.
healthPercentage = playerCurrentHealth / playerMaxHealth;
if (float.IsNaN (healthPercentage)) healthPercentage = 0;
if (healthPercentage > 1f) healthPercentage = 1f;
transform.localScale = new Vector3 (healthPercentage, 1, 1);
Your answer
Follow this Question
Related Questions
Should be easy..... 0 Answers
Change the scale with OnCollisionEnter 1 Answer
LocalScale Only works with part of my matrix 0 Answers
What's a difference between just Scale and localScale or Position and localPosition? 1 Answer
Possible to redefine the size of a model without scaling or using a 3D modeling program? 0 Answers