- Home /
Deactivate GameObject after Coroutine ended
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: http://answers.unity3d.com/questions/126783/can-i-check-if-a-coroutine-is-running.html 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();
}
}
}
You want to lerp all the way to alphavalue, so In the end t should be 1, but your for loop in the fade coroutine only goes up to 0.4f.
EDIT: Still it doesn't work. I tried this:
public class Options_script : $$anonymous$$onoBehaviour {
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;
bool CRFinished = true;
// Use this for initialization
void Start ()
{
OptionsCanvas.SetActive(false);
}
// Update is called once per frame
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(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.Get$$anonymous$$ouseButtonUp(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));
Deactivate();
}
}
IEnumerator Fade(float alphaValue, float alphaTime)
{
CRFinished = true;
float alpha = Options1.transform.GetComponent<Renderer>().material.color.a;
for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / alphaTime)
{
Color newColor = new Color(1, 1, 1, $$anonymous$$athf.Lerp(alpha, alphaValue, t));
Options1.transform.GetComponent<Renderer>().material.color = newColor;
yield return new WaitForEndOfFrame();
}
CRFinished = false;
}
void Deactivate()
{
if(CRFinished == false)
{
OptionsCanvas.SetActive(false);
}
}
}
No, since you divide Time.deltaTime by alphaTime when you increase time. You could also do:
for (float t = 0; t < alphaTime; t+=Time.deltaTime){ ...lerp(alpha,alphaValue,t/alphaTime);
if that makes more sense to you
Answer by MaxGuernseyIII · Sep 24, 2017 at 09:15 PM
You can add a parameter that lets you plug a little bit of behavior into the end of the coroutine.
IEnumerator Fade(float alphaValue, float alphaTime, Action onComplete)
{
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();
}
onComplete();
}
That said, this all seems like something you could just use animation to do. You can set them up to do callbacks and everything.
I'm sorry for not having answered before. I was trying to fix it with the solution the other guy provided. But I ended up doing it with animations which make it easier.
If anyone is trying to do this, do it with animation, it so much easier. And if you are still trying to do this with code, look at the responses from the first post.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Change the color of the string 2 Answers
surface shader homeworld (space) colorize transparent 1 Answer
Activating a prefab in the scene by find it over a tag? 1 Answer