- Home /
I want to change TextMeshProUGUI based on value of a float, but I cant seem to work it.
private TextMeshProUGUI txt1;
private float F1;
public void Start()
{
txt1 = GameObject.Find("Speed Text").GetComponent<TextMeshProUGUI>();
F1 = PlayerPrefs.GetFloat("SpeedFloat", 0.6f);//speedFloat increases by 0.1 when player powers up
}
public void ChangeTexts() //is called when player powers up
{
if (F1 == 0.6f) { txt1.text = "Speed Level 00"; }
if (F1 == 0.7f) { txt1.text = "Speed Level 01"; }
if (F1 == 0.8f) { txt1.text = "Speed Level 02"; }
}
but it doesnt work, if I use whole number as SpeedFloat like 1,2,3, it works, but not with decimal numbers like 0.6, 0.7, 0.8.
so I know it has something to do with decimal values/whole number thingy
it also doesnt work when I dont use f after every decimal float
public void ChangeTexts() //is called when player powers up
{
if (F1 == 0.6) { txt1.text = "Speed Level 00"; }
if (F1 == 0.7) { txt1.text = "Speed Level 01"; }
if (F1 == 0.8) { txt1.text = "Speed Level 02"; }
}
how do I make it work.
thanks in advance.
Do not check for equality of floats. Floating point precision is a thing and it's very likely that a number with decimal places will not be exactly equal.
If you really need that comparison you can try $$anonymous$$athf.Approximate ins$$anonymous$$d of "==", but it's a lot better if you just switch to ints.
You said that your speedFloat increases by 0.1, just make it increase by 1 and only when applying the speed, divide it by 10
yes, when I increase value by 0.1 I get 0.800002 ins$$anonymous$$d of 0.8
ok thanks for the help.
Your answer
Follow this Question
Related Questions
Enable ChildObject of Player with OnTriggerEnter? Or a whole different method? 0 Answers
Access mesh for TextMesh or GuiText 2 Answers
Converting float to int in GuiText (javaScript) 3 Answers
Trying to convert AutoType GUI Text script to Text mesh (3D text) 1 Answer
Getting pixel perfect text with either text meshes or GUI Text on a worldspace canvas 0 Answers