• 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

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Why is Unity showing the wrong variables in the Inspector? 1 Answer

Invoking a Function From A Variblized Script And Object 1 Answer

Cant get booleans and floats from other scripts 1 Answer

Incremental terrain height increase limited by float size 1 Answer

Changing a variable from another 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