Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 sk0071 · Feb 03, 2018 at 10:05 PM · coroutinecoloralphacoroutinescolor change

Trying to make sprite's alpha decrease to 0 in order to make it dip to black but it seems to be randomly glitching out

First things first here's the code:

 IEnumerator FadeToBlack() {
         Debug.Log ("hi");
         mySprite.color = GameObject.FindWithTag ("PlayerBase").GetComponent<ColourMaster> ().worldColour;
         Color TempC = mySprite.color;
         float speedFade = 5f;
         while (mySprite.color.a > 0f) {
             TempC.a -= (0.01f * speedFade);
             mySprite.color = TempC;
             yield return new WaitForSeconds (0.01f);
         }
     }

First of all, the coroutine isn't being spammed. The debug message only appears once, meaning the coroutine is only started once. After it does start, the object's sprite renderer (mySprite) has its color defined. Then some other thing are set, like a temp color variable. Then I start a while loop that runs while the alpha value is greater than 0. As TempC's alpha is lowered, mySprite.color is changed to TempC. In theory, this is supposed to lower the alpha of the sprite until it is 0. Next, the object is destroyed (a timer is elsewhere on my script and functions properly), independently of this coroutine. However, the observed effect is that the alpha value seems to pick random values every frame. Visually, this looks like the object's sprite is glitching out and flickering. Does anyone know how to fix this? Any help would be greatly appreciated.

Comment
Add comment · Show 1
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 Hurri04 · Feb 03, 2018 at 11:35 PM 0
Share

just as a small side-node: you might want to use

 mySprite.color = $$anonymous$$athf.Clamp01(TempC);

in line 8 to prevent the value from beco$$anonymous$$g lower than 0.

1 Reply

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

Answer by Glurth · Feb 03, 2018 at 11:03 PM

I suspect it has to do with something else in your project, or perhaps the way you are invoking it? I created the following test script decrease a color's alpha, and it worked as expected. Of course, this is the ONLY script in the test project, so I know nothing ELSE is changing the color. ( I also used just a member color variable, rather than another object's sprite's color, to simplify things- so the way you select the sprite might also have something to do with your issue.) But this should be proof enough that the coroutine is being invoked, when/as expected.

 public class corouttest : MonoBehaviour {
 
     public Color aColorValue;
 
     void Start () {
         StartCoroutine(FadeToBlack());
     }
     IEnumerator FadeToBlack()
     {
         Debug.Log("hi");
       //  mySprite.color = GameObject.FindWithTag("PlayerBase").GetComponent<ColourMaster>().worldColour;
         Color TempC = aColorValue;
         float speedFade = 5f;
         while (aColorValue.a > 0f)
         {
             TempC.a -= (0.01f * speedFade);
             aColorValue = TempC;
             Debug.Log("fading: " + aColorValue.a);
             yield return new WaitForSeconds(0.01f);
         }
     }
 }
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 sk0071 · Feb 04, 2018 at 01:44 AM 0
Share

Yeah you were right. I feel pretty dumb now. I have another script attached to the object in question which makes sure the objects color matches the world color. I forgot to disable it before running the coroutine. Did that and now it works fine.

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

80 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

Related Questions

UI Text not changing color [C#] 2 Answers

Coroutine issues when working with color.a 2 Answers

Get color of button and set to an object 1 Answer

When I try to change the Color of an Image, It will change to the original Image color instead.,When I change the color of an Image, it will show the original source image color instead 1 Answer

Flash image while ammo is low - coroutines? 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