Question by
Yetter · Jan 20, 2016 at 03:54 AM ·
alphafadeco-routine
Coroutine, object alpha fade in
Hey, writing a script for my shields, they should only be visible when triggered by a collider. working in c# so I've written it into a coroutine, after all my experimentation this was the only way I could make it work. It works but the problem is that the animation only triggers once even though the shield is still active after its been hit. I can keep on firing and the shield lose health but the animation will only trigger the very first time. Pretty new to coroutines and have no idea what an IEnumerator is or how yield is suppose to work but heres my code, hope someone can help
public class ShieldScript : MonoBehaviour {
public float fadeRate;
void Awake () {
gameObject.GetComponent<Renderer> ().enabled = false;
}
void Update () {
if (gameObject.GetComponent<Renderer>().enabled) {
StartCoroutine( shieldFade());
}
}
IEnumerator shieldFade(){
Color textureColor = gameObject.GetComponent<Renderer> ().material.color;
gameObject.GetComponent<Renderer> ().material.color = textureColor;
Debug.Log (textureColor.a);
textureColor.a += fadeRate*0.01f;
gameObject.GetComponent<Renderer> ().material.color = textureColor;
if (textureColor.a >= 0.1f) {
gameObject.GetComponent<Renderer> ().enabled = false;
yield break;
}
}
}
Comment