Question by
gallowscalibrator32 · Dec 29, 2020 at 08:32 PM ·
coroutinewaitforsecondsmethod
Waitforseconds doesn't work outside Start().
I have a coroutine that disable the GameObject it sits on after some time, but the WaitForSeconds don't work if i call it from another script or method, it only works if called by the Start method of the script it is in.
Coroutine:
public class CoroutineScript : MonoBehavior
{
public IEnumerator DisableAfterDelay(float delay)
{
yield return new WaitForSeconds(delay);
this.gameObject.SetActive(false);
}
}
Anything after the WaitForSeconds is not executed if this method isn't called in Start()
The Script i'm using to call the coroutine:
public class OtherScript : MonoBehavior
{
//code...
private void CallCoroutine()
{
StartCoroutine(scriptReference.DisableAfterDelay(3f));
}
//code...
}
Comment
Are you sure the object calling StartCoroutine
is not disabled / destroyed?
If so:
private void CallCoroutine()
{
scriptReference.StartCoroutine(scriptReference.DisableAfterDelay(3f));
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613022346im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with coroutines 1 Answer
Coroutine WaitForSeconds ignoring StopAllCoroutines... How can I do it? 0 Answers
How to implement pausing of WaitForSeconds based coroutine? 1 Answer
Call Coroutine step after step in foreach loop 0 Answers
Wait time after coroutine's wait seconds is complete 0 Answers