- Home /
activate an inactive GameObject with yield return new WaitForSeconds doesnt work
Hi,
I have the issue that an Object I set to inactive , wont get active again with yield return new WaitForSeconds.
What do I do wrong here? I call the IEnumerator Activate function but I cannot reach the code after WaitForSeconds?
Please help! I am struggling with this since two days and I tried so much already... I am out of ideas. Thanks!
public GameObject docu;
public GameObject document;
public GameObject reticle;
private void Start()
{
reticle = GameObject.Find("UI/ReticleCanvas/Reticle");
document = GameObject.Find("UI/Stats/Document");
docu = GameObject.FindGameObjectWithTag("Docu");
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
Destroy(docu);
document.SetActive(true);
Deactivate(reticle);
Destroy(document, 5);
// Code works perfectly until here!
StartCoroutine(Activate(reticle));
}
}
void Deactivate(GameObject g)
{
Debug.Log("GO INTO DEACTIVATE FUNCTION AND DEACTIVATE RETICLE!");
g.SetActive(false);
}
IEnumerator Activate(GameObject g)
{
Debug.Log("GO INTO ACTIVATE FUNCTION!");
// my trouble maker :(
yield return new WaitForSeconds(5);
Debug.Log("ACTIVATE RETICLE AFTER AFTER 5 SECONDS!");
g.SetActive(true);
}
Answer by Casiell · Oct 29, 2019 at 08:37 AM
Everything looks fine, so let's check all the common issues:
Is gameobject with this script active? Coroutines only run on enabled behaviours
Is your time scale greater than 0? WaitForSeconds time is modified by current time scale (default 1) so if you changed it, the time to wait will change as well (it will not go at all with time scale 0)
Your answer
Follow this Question
Related Questions
How to delete prefabs without warning popping up? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
*URGENT* Really Basic Script Problem 1 Answer
Why does my Lerp only run once? 0 Answers