Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 26, 2015 at 01:33 AM by Bunny83 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Stepepper · Jul 24, 2015 at 03:50 PM · errorfloatfloatingpoint

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..
alt text
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?

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Jul 24, 2015 at 04:19 PM 0
Share

?? 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.

avatar image Hexer · Jul 24, 2015 at 04:28 PM 0
Share

You are not somewhere using $$anonymous$$athf.Round in your script, are you?

avatar image Stepepper · Jul 24, 2015 at 04:36 PM 0
Share

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.

avatar image Runalotski · Jul 24, 2015 at 04:38 PM 1
Share

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

avatar image Bunny83 · Jul 24, 2015 at 04:52 PM 0
Share

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.

Show more comments

1 Reply

  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Stepepper · Jul 24, 2015 at 04:01 PM 0
Share

I did 8 decimals. And it just outputted 0.30000000. I still have no idea what's causing this.

avatar image Eric5h5 · Jul 24, 2015 at 04:47 PM 0
Share

The editor doesn't round any float output to the console, just those in Vector2/3/4.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges