Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by mellooow9 · Mar 28, 2020 at 11:26 PM · mathfloatwrongsubtractingsubtract

subtracting numbers gives a very wrong answer

Really not sure why this is happening. I'm logging the first number, the second number, and then the same numbers subtracted, but im getting an answer that doesn't make any sense.

my code:

 Debug.Log("first " + w[num].left.transform.InverseTransformPoint(array[i].transform.position).z);


                     Debug.Log("second " + w[num].left.transform.InverseTransformPoint(gameObject.transform.position).z);


                      Debug.Log("answer " + (w[num].left.transform.InverseTransformPoint(array[i].transform.position).z-w[num].left.transform.InverseTransformPoint(gameObject.transform.position).z));



An example of the output:

first 13.70678

second -6.305244

answer -2.603729

the answer should be around 20, but its -2.

Comment
Add comment · Show 3
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 sacredgeometry · Mar 29, 2020 at 01:17 PM 1
Share

Its really hard to read your code but the values could be being updated on another thread meaning that by the time you get to the third Debug.Log statement they have changed from the time you logged them out above.

Could you break your code into a more sensible/ readable structure please.

avatar image tormentoarmagedoom · Mar 29, 2020 at 01:36 PM 2
Share

Hello. If pretend to get an asnwer, dont post this unreadable code.. modyfy it to its essentials to be easy readen by peopple. want to be sure what is happening, dont debug.code the formula, create avriables so you can DEBUGG the code:

 float first = FOR$$anonymous$$ULA:
 float second = FOR$$anonymous$$ULA:
 float answer = first-second;

And debugg the code while running ot see the values of first and second at the answe line!

avatar image Bunny83 · Mar 29, 2020 at 02:23 PM 1
Share

Like others have said this code is barely readable and in general bad style. Also doing gameObject.transform is just redundant since every $$anonymous$$onoBehaviour does have a transform property directly. The next thing is are you sure there is absolutely no code between the two lines? Threads are not an issue since accessing transform.position or InverseTransformPoint can only be used on the main thread. Any attempt do use it on a different thread would cause an artificial error by the engine.


Next are you sure the numbers you've shown are the actual numbers and not just a part of the scientific notation?


Finally once again are you sure the code you've shown is exactly the code you used? Nothing altered and nothing in between the two lines?


Just like @tormentoarmagedoom I would also highly suggest you break down your lines into clearer and smaller sub parts. In general whenever you use a value that is obtained through a more or less complex process more than once you should cache it in a variable. It doesn't even help with debugging but also improves performance.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

135 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Float subtracting in increments of 1 no matter what value it's subtracted by 0 Answers

Subtraction gives completely wrong answers 1 Answer

Unity Float to db 2 Answers

if(12f > 12f) = true? 2 Answers

Bad result on a rest between 2 floats 1 Answer


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