Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 George_1999 · Sep 15, 2014 at 02:05 PM · not workingfloatif

if statement with floats in C# . what did i do wrong ?

So i wrote a simple if statement with float variables in C# that simply doesn't work i think i wrote everything right and i have no clue what's wrong . here's a screenshot of the code : http://oi57.tinypic.com/2u7vb4i.jpg . Thanks for your time

Comment
Add comment · Show 5
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 gjf · Sep 15, 2014 at 12:20 PM 0
Share

define "wrong".

it'll execute more than it should because you're checking for less than or equal to zero when just less than is enough. otherwise it's doing what you told it to.

wrong is also posting a screenshot ins$$anonymous$$d of the actual code - it's easier for us to help you if you post the relevant part of the script. (don't forget to use code tags - the faq/tutorial video will help explain things).

avatar image George_1999 · Sep 15, 2014 at 01:34 PM 0
Share

it doesn't execute the code that's inside the {} while the statement is true that's what's wrong .

avatar image gjf · Sep 15, 2014 at 01:39 PM 0
Share

but it's getting to Update()?

avatar image Landern · Sep 15, 2014 at 02:23 PM 0
Share

There isn't anything wrong with your if statement. Try adding some debugging statements that you can read in the console:

 //... previous code
 void Update() {
   HealthBarScale -= 0.005f;
     
     Debug.Log(string.Format("In PlayerInfo Update method, Current Value of HealthBarScale: {0}", HealthBarScale));
     if (HealthBarScale <= 0.00f)
     {
         Debug.Log(string.Format("HealthBarScale is less than or equal to 0.00, its current value is: {0}, forcing 0.00f as HealthBarScale value.", HealthBarScale));
         HealthBarScale = 0.00f;
     }
     else
     {
         Debug.Log(string.Format("HealthBarScale is greater than 0.00, its current value is: {0}, no changes to variable at this time.", HealthBarScale));
     }
     
     healthBar.localScale = new Vector3(HealthBarScale, 0, 0);
 }

You should also look into using $$anonymous$$athf.Clamp to clamp the value to 0.

avatar image Zamanis · Sep 15, 2014 at 02:30 PM 0
Share

Also if you HealthBarScale -= 0.005f; in every update() it ll be 0 form the first 2 frames... and it will go - really fast;

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Error when checking if float is a whole number 2 Answers

How do you keep spheres from floating into space? Rigidbody not working. 0 Answers

How do I make my own Remote Settings backend like Unity Analytics Remote Settings? 0 Answers

Can't change a float 2 Answers

Can I create a list with an int/float and a string? C# 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