• 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 $$anonymous$$ · Jul 20, 2018 at 06:12 PM · coloralpha

Coroutine issues when working with color.a

So I am trying to make a sprite become less transparent over a period of time, and it becomes more visible once, but fails to do so again, resulting in the sprite being barely visible on the screen. Whats supposed to happen is that the sprite keeps getting more visible every few moments until its alpha has reached 1.

This is my code:

 public class ScreenFace : MonoBehaviour {
 
     public GameObject face;
 
     private void Start()
     {
         ColorAplhaChange(0);
 
         StartCoroutine ("show");
     }
 
     private void ColorAplhaChange(float a )
     {
         Color NewColor = face.GetComponent<SpriteRenderer>().color;
         NewColor.a = a; 
         face.GetComponent<SpriteRenderer>().color = NewColor;
     }
 
 
 
     IEnumerator show () {
         float b = 0.1f; 
         yield return new WaitForSeconds (b); 
         ColorAplhaChange(+0.01f); 
         yield return new WaitForSeconds (b); 
         ColorAplhaChange(+0.01f); 
         yield return new WaitForSeconds (b); 
         ColorAplhaChange(+0.01f); 
         yield return new WaitForSeconds (b); 
         ColorAplhaChange(+0.01f); 
         yield return new WaitForSeconds (b); 
         ColorAplhaChange(+0.01f); 
         yield return new WaitForSeconds (b); 
         ColorAplhaChange(+0.01f); 
         yield return new WaitForSeconds (b); 
         ColorAplhaChange(+0.01f); 
         yield return new WaitForSeconds (b); 
         ColorAplhaChange(+0.01f); 
         yield return new WaitForSeconds (b); 
         ColorAplhaChange(+0.01f); 
         yield return new WaitForSeconds (b); 
         ColorAplhaChange(+0.01f); 
         yield return new WaitForSeconds (b); 
     }
 }
 

Anyone know whats going wrong?

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
2
Best Answer

Answer by Stratosome · Jul 20, 2018 at 10:48 PM

Hi there!

So, I think the problem here is that your function, ColorAplhaChange (I assume you meant 'Alpha'), takes in a new alpha value and sets the sprites alpha to that. This means that your coroutine, 'show', is simply setting the sprites alpha to 0.1f 10 times. That isn't going to smoothly change it or anything. So, what you want to do is to modify the existing alpha by some amount to get to a new alpha and keep going like that. Here's a nifty little coroutine I quickly threw together for ya that gets the job done. Give it a final alpha (the alpha you want it to go to) and a fade duration (how long it should take to fully fade):

 private IEnumerator FadeSprite(float finalAlpha, float duration) {
 
         // Get some basic data
         Color initialColor = mySpriteRenderer.color;
         float elapsedTime = 0;
         float changeInAlpha = finalAlpha - mySpriteRenderer.color.a;
 
         // While there is still time left on the "timer"
         while (elapsedTime < duration) {
             // Set the sprite color
             float percentageComplete = (elapsedTime / duration);
             float newAlphaAdjustment = changeInAlpha * percentageComplete;
             mySpriteRenderer.color = new Color(initialColor.r, initialColor.g, initialColor.b, initialColor.a + newAlphaAdjustment);
 
             // Updated time passed
             elapsedTime += Time.deltaTime;
             yield return null;
         }
 
         // Makes sure that the alpha is exactly what you wanted
         mySpriteRenderer.color = new Color(initialColor.r, initialColor.g, initialColor.b, finalAlpha);
     }


Hope that helps!

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 $$anonymous$$ · Jul 21, 2018 at 02:10 AM 0
Share

Ah thanks, this appears to work! What I was attempting to do was add 0.1f to the alpha ten times, I didnt suspect it would set it to that amount ten times ins$$anonymous$$d. So your explanation was very helpful I must say.

avatar image
0

Answer by $$anonymous$$ · Jul 21, 2018 at 07:41 PM

Found that this also worked, but Stratosomes answer is more effichent probably, and less ugly to look at in code form.

 public class ScreenFace : MonoBehaviour {
 
     public GameObject face;
 
     private void Start()
     {
         ColorAplhaChange(0);
 
         StartCoroutine ("show");
     }
 
     private void ColorAplhaChange(float a )
     {
         Color NewColor = face.GetComponent<SpriteRenderer>().color;
         NewColor.a = a; 
         face.GetComponent<SpriteRenderer>().color = NewColor;
     }
 
     public bool willdo; 
 
     IEnumerator show () {
         if (willdo == true)
         {
             float b = 5f;
             yield return new WaitForSeconds(b);
             ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
             yield return new WaitForSeconds(b);
             ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
             yield return new WaitForSeconds(b);
             ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
             yield return new WaitForSeconds(b);
             ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
             yield return new WaitForSeconds(b);
             ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
             yield return new WaitForSeconds(b);
             ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
             yield return new WaitForSeconds(b);
             ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
             yield return new WaitForSeconds(b);
             ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
             yield return new WaitForSeconds(b);
             ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
             yield return new WaitForSeconds(b);
             ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
             yield return new WaitForSeconds(b);
             ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
             yield return new WaitForSeconds(b);
             ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
             yield return new WaitForSeconds(b);
             ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
             yield return new WaitForSeconds(b);
             ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
             yield return new WaitForSeconds(b);
             ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
             yield return new WaitForSeconds(b);
             ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
             yield return new WaitForSeconds(b);
             ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
             yield return new WaitForSeconds(b);
         }
     }
 }
 
Comment
Add comment · 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

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

86 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 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 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 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

renderer.material.color not being changed? 2 Answers

Cannot set UnityUI.Image opacity when place on a UnityUI.Button 2 Answers

Alpha Problems With Render Textures 0 Answers

Default color issue 2 Answers

Color.a won't be shown/set correctly for values between 1 an 244 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