Hi!
I have this code that i am using for fading out gui panels (made with NGUI), the problem is that code in update works just fine but when i want to do it through coroutine it just executes once?
here is the code (i comment out StartCoroutine part and comment Update to execute through coroutine)
using UnityEngine;
using System.Collections;
public class UnfadePanel : MonoBehaviour
{
public float duration = 1.5f;
float mStart = 0f;
UIWidget[] mWidgets;
void Start ()
{
mWidgets = GetComponentsInChildren<UIWidget>();
foreach (UIWidget w in mWidgets)
{
Color c = w.color;
c.a = 0f;
w.color = c;
}
//StartCoroutine(Unfade());
}
IEnumerator Unfade()
{
mStart += Time.deltaTime * (1f / duration);
Debug.Log(mStart.ToString());
float alpha = Mathf.Lerp(0f,1f,mStart);
foreach (UIWidget w in mWidgets)
{
Color c = w.color;
c.a = alpha;
w.color = c;
}
if(alpha == 1.0)
Destroy(this);
yield return null;
}
void Update ()
{
mStart += Time.deltaTime * ( 1 / duration );
float alpha = Mathf.Lerp(0f,1f,mStart);
foreach (UIWidget w in mWidgets)
{
Color c = w.color;
c.a = alpha;
w.color = c;
}
if(alpha == 1.0f)
Destroy(this);
}
}
any help appreciated!