using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DisappearingImage : MonoBehaviour {

    public Image image;
    public float fadeOutTime = 4.0f;
    public float waitTime;

	// Use this for initialization
	void Start () {
        StartCoroutine(FadeImage(fadeOutTime, waitTime));	
	}

    IEnumerator FadeImage(float _fadeOutTime, float _waitTime = 0) {
        if (!image)
            yield break;

        yield return new WaitForSecondsRealtime(_waitTime);

        for (float t = 0.0f; t <= 1.0f; t += Time.unscaledDeltaTime / _fadeOutTime) {
            float a = Mathf.Lerp(1, 0.0f, t);
            Color c = image.color;
            c.a = a;
            image.color = c;
            yield return null;
        }

        Color co = image.color;
        co.a = 0;
        image.color = co;

        image.gameObject.SetActive(false);
    }
}
