Hi!
I need help trying to deactivate an object after its coroutine ends. When the coroutine ends the object alpha color should be equal to 0. I though about checking the color of the object, and I also tried this: Can i check if a coroutine is running? - Questions & Answers - Unity Discussions
but none of those worked for me. Please help me.
This is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Options_script : MonoBehaviour {
public GameObject OptionsCanvas;
public GameObject Options1;
public GameObject Options2;
public GameObject Options3;
public GameObject Options4;
float downclick, upclick = 0f;
float delay = 0.6f;
bool ready = false;
bool CanvasSetActive = false;
// Use this for initialization
void Start ()
{
OptionsCanvas.SetActive(false);
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) && ready == false)
{
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);
if (hit.collider != null)
{
if (hit.collider.name == "Options")
{
downclick = Time.time;
ready = true;
}
}
}
if (Input.GetMouseButtonUp(0) && ready == true)
{
Vector2 secondworldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D secondhit = Physics2D.Raycast(secondworldPoint, Vector2.zero);
if (secondhit.collider != null)
{
if (secondhit.collider.name == "Options")
{
upclick = Time.time;
ready = false;
}
}
}
if ((upclick - downclick) > delay)
{
CanvasSetActive = true;
OptionsCanvas.SetActive(true);
if(CanvasSetActive == true)
{
StartCoroutine(Fade(1f, 0.4f));
}
}
else
{
CanvasSetActive = false;
StartCoroutine(Fade(0f, 0.4f));
}
}
IEnumerator Fade(float alphaValue, float alphaTime)
{
float alpha = Options1.transform.GetComponent<Renderer>().material.color.a;
for (float t = 0.0f; t < 0.4f; t += Time.deltaTime / alphaTime)
{
Color newColor = new Color(1, 1, 1, Mathf.Lerp(alpha, alphaValue, t));
Options1.transform.GetComponent<Renderer>().material.color = newColor;
yield return new WaitForEndOfFrame();
}
}
}