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 Wineburgen · Oct 06, 2015 at 03:51 PM · c#if statement

If statement is being ignored

Hi,

Somehow this part of my code is being ignorded: && Difference < 0.5f When I use == instead of > it functions properly.

 void Update ()
               {
         float Difference = player1.transform.position.x - transform.position.x;
             if (Input.GetKeyDown (KeyCode.Space) && Difference > 0.5f) 
             {
                                 Debug.Log (player1.transform.position.x);
                 Debug.Log (transform.position.x);
                 Debug.Log (Difference);
                          }
                }

The Debug.Log (Difference) shows that when Difference > 0.5f the if statement still is 'true'.

Can somebody please help me with my problem?

Thanks in advance!

Comment
Add comment · Show 4
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 Bunny83 · Oct 07, 2015 at 10:51 AM 1
Share

You said it's still "true". How do you know that? Because of the Debug.Logs? You should use meaningful Debug.Logs

 Debug.Log("$$anonymous$$eyDown: " + gameObject.name + " Diff: " + Difference + " Px: " + player1.transform.position.x + " Tx: " + transform.position.x, gameObject);

With that statement you get:

  • all combined in one output so it isn't mixed with other Debug logs

  • It has a description so it can be differentiated from other Debug.Logs

  • It also prints the name of the gameobject this script is attached to in case you have it on multiple gameobjects.

  • finally we pass "gameObject" as second "contect" parameter to Debug.Log. That should allow you by clicking (or double clicking?!?) on the Debug,Log in the console to highlight the object responsible

From your comment(s) below it seems your actual code looks different since you have admit that you have a typo in your code (which you still haven't fixed in your question). So are you sure that the code you posted really represents your actual code?

ps: There's an "edit" button in the menu on the top right of your question.

avatar image Wineburgen Bunny83 · Oct 07, 2015 at 04:04 PM 0
Share

Thanks for your comment. I have not much experience with c# and what you are $$anonymous$$ching me about the functions of the Debug.Log is very useful! I'll hope I get it right now:)!

avatar image DiegoSLTS · Oct 07, 2015 at 01:21 PM 0
Share

Have you checked that the script is not attached to another object in your scene? $$anonymous$$aybe the "if" is true for that object. Add a line like the next one to make sure it happens in the object you think it's happening:

 Debug.Log(name);
avatar image Wineburgen DiegoSLTS · Oct 07, 2015 at 04:07 PM 0
Share

I only have a floor, a sphere and a cylinder (the player), and the script is only attached to the sphere.. Thanks anyways, could be a very stupid mistake that could have taken me hours to solve;)

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Dave-Carlile · Oct 06, 2015 at 03:53 PM

The Debug.Log (Difference) shows that when Difference > 0.5f the if statement still is 'true'

Isn't that what you have coded? "If space is pressed and Difference > 0.5f then do something". If the log is showing that Difference is > 0.5, then "Difference > 0.5)" is going to be true every time.

Comment
Add comment · Show 6 · 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 Wineburgen · Oct 06, 2015 at 03:58 PM 0
Share

Sorry, was a typo in my question on this site. In my script it is: && Difference < 0.5f

avatar image Pharan Wineburgen · Oct 06, 2015 at 04:47 PM 0
Share

Are you getting an error in the case where the difference is negative? Because -1 < 0.5 is true but may not be the logic you're looking for.

avatar image Wineburgen Pharan · Oct 06, 2015 at 04:50 PM 0
Share

No, Difference starts at 5 as is increasing over time...

avatar image Dave-Carlile Wineburgen · Oct 06, 2015 at 05:37 PM 0
Share

I've had Debug.Log format numbers for me (don't recall under what circumstances), but is Difference just displaying as 0.5f in the log, but it's really 0.49999f or something? I'd go with setting a break point on that line and do a watch on the value once you get there and see what the actual value is. Or try using string.Format when logging the value to make sure you're getting all of the decimal places. e.g. Debug.Log(string.Format("{0:0.00000000}", Difference)) or something like that.

avatar image Wineburgen Dave-Carlile · Oct 07, 2015 at 09:25 AM 0
Share

$$anonymous$$y Difference value is on a certain moment about 50, and the if statement is still true (according to c#). Does it matter under those circumstances whether my float is 0.5 or 0.4999?

Show more comments

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

C# if statement not working!! 1 Answer

Both sides of my IF statement seem to be firing 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