- Home /
Coroutines /WaitForSecond ain't working
i applied the coroutine to my code but i found that my code after ( yield return new WaitForSeconds();
) isn't working So i created a simple croutine got only print functions to test how it works
here it is :
void OnTriggerEnter2D (Collider2D x)
{
Debug.Log("OnTriggerEnter2D has been called");
if (x.gameObject.tag == "Player") {
Debug.Log("gameObject.tag = Player");
StartCoroutine (speedUp());
Debug.Log("Coroutine started");
}
}
IEnumerator speedUp(){
print ("message delayed for 3 sec");
yield return new WaitForSeconds (3f);
print("hello world");
it should after 3 seconds display hello world , unfortunately it is not showing hello world also for the test purpose i tried to set different values to WaitForSeconds
i tried 0 and it worked but for sure there is no delay i tried 0.003 it worked but also there is no noticeable delay i checked the time manager and tried different values but nothing worked i tried WaitForSecondsRealime()
it ain't working
also i tried to test the same coroutine in a new project and it worked fine as i want debug.log(" Hello world");
delayed 3 seconds exactly
Are you sure the object that is calling the coroutine isn't being destroyed?
@karmal225 there's nothing wrong with the code you are showing. In other words, the code you show does not explain the problem you are having.
This is quite common actually. You think the problem is there, but it's somewhere else.
As @sleepandpancakes says in his comment, it is very likely that the object calling the coroutine is getting destroyed, OR the object containing the coroutine is getting destroyed.
But this is just guesswork. If you don't post additional information, I doubt very much that you'll be able to get much more help.
Answer by Bunny83 · Jun 25, 2017 at 01:03 AM
The problem is that you either destroy your game object where that script is attached to, or you deactivate the game object. In both cases you will cancel the coroutines which run on that script instance. A coroutine is always bound to the script instance which you used for your StartCoroutine call.
Exactly thats what i found there is another trigger object destroy object when it enters so the coroutine doesn't resume because the game object destroyed Thank you
Answer by Nixmortem · Jun 24, 2017 at 08:26 PM
Whenever I use an IEnumerator I have to put what I actually want to happen in a different function and call it from the IEnumerator. Like this:
void OnTriggerEnter2D (Collider2D x)
{
Debug.Log("OnTriggerEnter2D has been called");
if (x.gameObject.tag == "Player") {
Debug.Log("gameObject.tag = Player");
print ("message delayed for 3 sec");
StartCoroutine (speedUp());
Debug.Log("Coroutine started");
}
}
private void PrintMessage(){
print("hello world");
}
IEnumerator speedUp(){
while(true){
PrintMessage();
yield return new WaitForSeconds(3);
}
That's how I normally use it for my Idle games that rely on Coroutines heavily. Hopefully this helps you out! :)
@Nixmortem it's not necessary to put code in a different function. It works quite well to put it inline inside the IEnumerator. In other words this is not the problem here.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
IEnumerator's did not read "bool" after yield return new WaitForSeconds. 1 Answer
Reset WaitForSeconds Coroutine 1 Answer