Hi, I’ve been trying to make a ending screen, but something went wrong. When I start the programm, the fade in and fade out animation don’t work. But if I don’t invoke fade out animation, fade in works. Help me please! Here is the code:
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
public class ExitClassic : MonoBehaviour
{
public GameObject dayEndBG;
public GameObject dayEndT;
public GameObject dayEndT1;
public GameObject dayEndT2;
public Text daysL;
public Text coinsN;
private Color bgC;
private SpriteRenderer bgRend;
private SpriteRenderer bgRend1;
private int d = 3;
private int cD = 1;
private int qN = 100;
private bool dayEnd = false;
private ScoreClassic script;
void Start()
{
bgRend = dayEndBG.GetComponent<SpriteRenderer>();
bgRend1 = dayEndBG.GetComponent<SpriteRenderer>();
bgC = bgRend.color;
daysL.text = "Quote: " + d + " days";
coinsN.text = qN + " coins";
}
void Update()
{
if(dayEnd == true){
cD++;
StartCoroutine(FadeIn());
Invoke("TextShow", 3f);
if(cD == 3){
if(script.Scr >= qN){
Invoke("TextShowW", 4f);
} else{
Invoke("TextShowL", 4f);
}
cD = 1;
}
Invoke("CleanT", 4f);
StartCoroutine(FadeOut());
dayEnd = false;
}
}
public void EXIT()
{
Debug.Log("left");
dayEnd = true;
}
private IEnumerator FadeIn()
{
float alphaVal = bgRend.color.a;
Color tmp = bgRend.color;
while (bgRend.color.a < 1)
{
alphaVal += 0.01f;
tmp.a = alphaVal;
bgRend.color = tmp;
yield return new WaitForSeconds(0.02f);
}
}
private void TextShow()
{
dayEndT.SetActive(true);
}
private void TextShowW()
{
dayEndT.SetActive(false);
dayEndT1.SetActive(true);
}
private void TextShowL()
{
dayEndT.SetActive(false);
dayEndT2.SetActive(true);
}
private IEnumerator FadeOut()
{
float alphaVal1 = bgRend1.color.a;
Color tmp1 = bgRend1.color;
while (bgRend1.color.a >= 0)
{
alphaVal1 -= 0.01f;
tmp1.a = alphaVal1;
bgRend1.color = tmp1;
yield return new WaitForSeconds(0.02f);
}
}
private void CleanT()
{
dayEndT.SetActive(false);
dayEndT1.SetActive(false);
dayEndT2.SetActive(false);
}
}