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
5
Question by GutoThomas · Mar 09, 2012 at 04:53 PM · c#coloralphatransparent

Slowly fades from opaque to alpha

Hey guys, I'm trying to make a material slowly changes it opacity. It's not working well because I only see my material when it's totally opaque or totally transparent(I can't see the fade effect) . Here's the code. Can someone tell me what I'm doing wrong?

 using UnityEngine;
 using System.Collections;
 
 public class alphaControl : MonoBehaviour {
     
     void Update () {
         
         
         if(Input.GetKeyUp(KeyCode.T)) {
             
             ToAlpha();
             
         }
         
         if(Input.GetKeyUp(KeyCode.F)) {
             
             fromAlpha();
             
         }
     
     }
     
     void ToAlpha () {
         
         float alpha = transform.renderer.material.color.a;
         
         while(alpha > 0) {
         
             alpha -= Time.deltaTime;
             print (alpha);
             Color newColor = new Color(1, 1, 1, alpha);
             transform.renderer.material.color = newColor;
             
         }
         
     }
     
     void fromAlpha () {
         
         float alpha = transform.renderer.material.color.a;
         
             while(alpha < 1) {
             
             alpha += Time.deltaTime;
             print (alpha);
             Color newColor = new Color(1, 1, 1, alpha);
             transform.renderer.material.color = newColor;
             
         }
         
     }
 }

Any help would be appreciated! Thanks from now.

Guto

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
27
Best Answer

Answer by Bunny83 · Mar 11, 2012 at 03:22 AM

Your code doesn't make much sense since your loop is executed without delay. You might want to use a coroutine instead. Also there's no need for two functions:

 void Update ()
 {
     if(Input.GetKeyUp(KeyCode.T))
     {
         StartCoroutine(FadeTo(0.0f, 1.0f));
     }
     if(Input.GetKeyUp(KeyCode.F))
     {
         StartCoroutine(FadeTo(1.0f, 1.0f));
     }
 }
 
 IEnumerator FadeTo(float aValue, float aTime)
 {
     float alpha = transform.renderer.material.color.a;
     for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime)
     {
         Color newColor = new Color(1, 1, 1, Mathf.Lerp(alpha,aValue,t));
         transform.renderer.material.color = newColor;
         yield return null;
     }
 }

FadeTo will fade towards the value you pass in the time you specify. In the example above 1 sec.

If you want it to fade faster, just reduce the time:

 StartCoroutine(FadeTo(1.0f, 0.5f));
Comment
Add comment · Show 10 · 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 GutoThomas · Mar 11, 2012 at 07:44 AM 0
Share

Thanks! That's exactly what I'm looking for! Thank you again! (:

avatar image MD_Reptile · May 25, 2012 at 01:10 AM 0
Share

what was delta for?

avatar image Bunny83 · May 25, 2012 at 10:11 AM 0
Share

@$$anonymous$$DReptile: $$anonymous$$y bad ;) The first solution used this value to deter$$anonymous$$e the "direction" needed to get from the current value to the target value.

With the Lerp solution it's not needed anymore.

I will remove it, thanks for the hint ;)

avatar image Bunny83 · Apr 21, 2015 at 03:41 AM 2
Share

@Partomo:
Well he probably wants to use the color "white". If you want to keep the color the material has you have to use

 Color newColor = transform.renderer.material.color;
 newColor.a = $$anonymous$$athf.Lerp(alpha,aValue,t);
 transform.renderer.material.color = newColor;

"color" is a property, so you can't assign color.a directly. This would just invoke the "get" method of the color. So you get a copy of the color struct and than you change the alpha of that copy. This wouldn't change the actual color at all. You have to use a temp variable and assign it back to the property so the set-method get called.

avatar image tswalk · Oct 12, 2015 at 06:28 AM 1
Share

just a note, I noticed that the coroutine returning null will cause it to get out of sync at times.. ins$$anonymous$$d, replace with "yield return new WaitForEndOfFrame();" to fix a strobe effect that may occur. I think that shows up when you have many items that may be running the coroutine... in my case, I had ~20 objects with large textures (0.7 $$anonymous$$B) covering quite a bit of screen space. this fixed the issue.

avatar image josetteseitz tswalk · Jul 25, 2020 at 02:11 PM 0
Share

thanks for this! I replaced null with WaitForEndOfFrame() and it's looking a bit smoother and less frames to process.

Show more comments
avatar image
0

Answer by ElRhino · Mar 09, 2012 at 05:44 PM

A non code solution might be to use the animation editor to fade out the alpha. You are able to set a key frame from 0%, and for 100%, and it will handle the fade for you. (and if you set the animation to ping pong, once it reaches the end it will go backwards, and then start again) Sorry I don't know much about the programming side.

Comment
Add comment · Show 1 · 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 GutoThomas · Mar 11, 2012 at 01:20 AM 0
Share

That would be a way to do it, but I really, in first case, would want to do it by a code(Don't matter if is in C# or JS). But thanks for the reply! Someone else?

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

14 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

Related Questions

Distribute terrain in zones 3 Answers

How to add color/alpha control to this shader? 0 Answers

Default color issue 2 Answers

Multiple Cars not working 1 Answer

Object opacity/alpha? 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