• 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 /
  • Help Room /
avatar image
Question by K-Skanz · Jun 12, 2016 at 04:38 AM · scripting problemwebplayervariablesfloatfor-loop

For with float i returning wrong number.

Hi, I want to make a "For" like this:

 for (float i = 0; i <= 1; i+=0.1F)
         {
             BSMaterial.color = new Color(BSColor.r, BSColor.g, BSColor.b, BSColor.a - i);
             yield return new WaitForSeconds(0);
             Debug.Log(BSMaterial.color.a);
         }

but it gains a Wrong value when I = 0.7, then "I" comes with "0.6900001" and all values after this comes wrong making an infinite loop.

Mathf.Round don't worked for me, since Mathf.Round round a number to 1.

Sorry if I said something wrong, and thanks for helping me.

Comment

People who like this

0 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 NoseKills · Jun 12, 2016 at 11:58 AM 0
Share

Looping i like that does not create an infinite loop as demonstrated here.

Floating point accuracy will cause slight differences to values of i compared to what you'd expect. You should google and read about floating point accuracy

avatar image K-Skanz NoseKills · Jun 12, 2016 at 12:24 PM 0
Share

In that case my "i" ends like -6.3452e+19, but my next line will sub -0.01F making it an eternity, I found a poor solution, like that:

 float alpha = 1;
    for (float i = 0; i <= 1; i+=0.01F)
             {
                 alpha -= i;
                 BSMaterial.color = new Color(BSColor.r, BSColor.g, BSColor.b, alpha);
                 yield return new WaitForSeconds(0);
             }
             alpha = 0;
             yield return new WaitForSeconds(3);
             for (float i = 0; i <= 1; i += 0.01F)
             {
                 alpha += i;
                 BSMaterial.color = new Color(BSColor.r, BSColor.g, BSColor.b, alpha);
                 yield return new WaitForSeconds(0);
             }
             alpha = 1;

I don't like that, but I don't know why that don't stop at "-1. something", I googled it and no one says why this doesn't stop.

avatar image NoseKills K-Skanz · Jun 12, 2016 at 01:53 PM 1
Share

You can do the loop with an int and calculate the alpha from that.

The way you do things now the fade time depends on framerate. You might also consider tracking Time.time, comparing it to a duration you predefine and calculating alpha from that.

Show more comments

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Create "Optional" public variable that won't throw an error 1 Answer

Unexpected symbol 'float' 1 Answer

Variable from another script isn´t updating 3 Answers

Damage Clone From Projectile Script 0 Answers

Problem with the Inspector and Script 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