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.