- Home /
Problem is not reproducible or outdated
Float check not returning the right value.
Something very weird is going on, I'm trying to check if a float called fireDelay is lower than 0. Which, according to what I programmed, it is. But it seems that it doesn't work. I made a boolean check that writes in the Debug.Log, and the actual float value that writes in the debug log.
public float fireDelay = 0.0f;
public void PrimaryFire(RaycastHit hit)
{
bool fireDelayLower = fireDelay <= 0.0f;
Debug.Log("fireDelayLower = " + fireDelayLower);
Debug.Log(fireDelay);
if (fireDelay <= 0.0f)
{
Debug.Log(pickupOwner.GetComponent<AudioSource>(), pickupOwner);
this.fireDelay = fireRate;
pickupOwner.GetComponent<AudioSource>().PlayOneShot(gunSound[Random.Range(0, gunSound.Length - 1)]);
}
if (currentClip <= 0)
{
DryFire();
return;
}
}
Output:
fireDelayLower = False
0.3
And in the inspector itself..
I understand that it's -0.02etc because of the floating point error. But shouldn't it be -0.00000001 instead?
What exactly is going on here and how do I fix it?
?? Those numbers look fine to me.
Clearly, somewhere in your code, you're setting it to 0.3.
If you want to know why it's landing on -0.028, just print it every frame (something is making it go down slowly, right? Print it then.) That should make it obvious what's happening.
You are not somewhere using $$anonymous$$athf.Round in your script, are you?
Clearly, somewhere in your code, you're setting it to 0.3.
Nowhere, that is the weird part.
something is making it go down slowly, right?
Yeah, in the update function.
You are not somewhere using $$anonymous$$athf.Round in your script, are you?
Nope.
What is fire rate set to because on line 12. you set fireDelay to fireRate so if fireRate is 0.3 then that could be it
and i can't see any code that would reduce fireDelay below 0
There's an easy trick to see where, when and who changes your variable. Just replace the variable temporarily with a property:
private float m_fireDelay = 0.0f;
public float fireDelay
{
get {return m_fireDelay;}
set
{
Debug.Log("fireDelay changed! old value: " + m_fireDelay + " new value: " + value);
m_fireDelay = value;
}
}
Now everytime the value get changed somewhere you get a debug output with the old and the new value. Since every debug.log entry has a stack trace you can see exactly from where it got set to that value.
Answer by Cherno · Jul 24, 2015 at 03:57 PM
The Unity editor automatically rounds any float that is output by the console; I suspect it applies the ToString method internally, which rounds to one decimal. Anyway, you can make the console output the verbose float like this:
Debug.Log(fireDelayLower.ToString("F4"));
Where F4 indicates the number of decimals you want to show.
See Standard Numeric Format Strings for further information.
I did 8 decimals. And it just outputted 0.30000000. I still have no idea what's causing this.
The editor doesn't round any float output to the console, just those in Vector2/3/4.
Follow this Question
Related Questions
Mathf.Lerp float between 0 and 1 based on boolean input 3 Answers
IComparable error due to float 1 Answer
Cannot implicitly convert type 'float' to 'bool' 3 Answers
why the debree timer float not work 1 Answer
BCE0023 error 2 Answers