- Home /
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.
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.
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);
}
}
}
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
Follow this Question
Related Questions
UI Text not changing color [C#] 2 Answers
Coroutine issues when working with color.a 2 Answers