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 pickle chips · Apr 22, 2014 at 03:25 AM · c#transparencyalphafadefadeout

Fading out doesn't fade out, but instantly goes from black to clear

Hi, so i'm trying to get a fading out effect, but as I have it now it doesn't fade, but instead it instantly goes from black to clear after a certain period of time.

Here is my code:

 public float fadeSpeed = 5;
 private Material mat;

 public float alpha = 255f;
 // Use this for initialization
 void Start () {
     mat = gameObject.renderer.material;
 }
 
 // Update is called once per frame
 void Update () {
     if (alpha > 0)
         alpha -= fadeSpeed*(float)Time.deltaTime;
     else if (alpha < 0)
         alpha = 0;

     mat.color = new Color (0, 0, 0, alpha);
 }

So I have this script attached to a plane which is placed in front of the camera. The alpha variable is only public so i can see it in the editor for now, and it does decrease like it's supposed to. But the plane doesn't get any clearer, not until the alpha is 0 it instantly changes but doesn't fade. can anyone tell me what's wrong here? The plane's material is Transparent/diffuse in case that matters.

Any help is appreciated, thanks.

Comment
Add comment · Show 6
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 pickle chips · Apr 22, 2014 at 03:38 AM 0
Share

The transparent diffuse shader. If I manually drag the alpha bar in the editor it fades away properly, so I dont believe its the shader.

avatar image Slowmanrunning · Apr 22, 2014 at 03:44 AM 1
Share

The 'Color' class isn't based on values of 0-255, it's based on a float of 0f-1f

http://docs.unity3d.com/Documentation/ScriptReference/Color.html

The reason it fades instantly is because from 255-1 it's fully opaque, then from 1 to 0 it fades (almost instantly).

Try changing the relevant line to: public float alpha = 1f; and either greatly reduce fadeSpeed or divide it by 255 if you want it to keep its current magnitude.

avatar image pickle chips · Apr 22, 2014 at 03:50 AM 0
Share

Oh you know what you're probably right. I ran in to this same problem a while back as well, hopefully this time I'll remember for for the future. I'll fix this tomorrow, im sure thats the problem, thanks. Although, is there not some alternative color class that does utilize the 255 system? If I recall correctly thats what I ended up doing last time.

Anyways thanks, I'll try it out tomorrow and if it works I'll let you know, and you can post your comment as an answer.

avatar image Benproductions1 · Apr 22, 2014 at 07:46 AM 0
Share

@pickle chips, Both OpenGL and DirectX work with floating point color values. Not only are they more accurate than 8 bit color values, but they are much easier to mix and do awesome stuff with. There is no reason Unity should make such a class, but you're welcome to make your own of course ;)

avatar image fafase · Apr 22, 2014 at 07:21 PM 1
Share

Add to that, you are using deltaTime with a speed of 5:

  alpha -= fadeSpeed*(float)Time.deltaTime;


which means your whole fading is done in 0.2s which is not invisible but fairly fast.

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.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Distribute terrain in zones 3 Answers

Fading all but single instance of a button 1 Answer

Fading out a transparent material 1 Answer

Fading Objects does not work (Unity 5) 0 Answers

Transparency not working in Windows build 3 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