HI, I am using a simple coroutine: IEnumerator nextFigureTimer() { yield return new WaitForSeconds (5f); Debug.Log("Coroutine executed"); prepareNextFigure = true; } and somewhere in Update I call it: Debug.Log("Before calling coroutine"); StartCoroutine(nextFigureTimer()); but it is not executed every time, At some point I can't see the message "Coroutine executed", while I see "Before calling coroutine" every time. And my game is not working properly because prepareNextFigure flag is not updated. Does Someone knows the reason why this is happening? And what the appropriate solution will be ? Thanks
You have to be careful about calling a coroutine in update because it is calling it every frame unless you set boolean flags to stop it. So you are calling it 60 times per second. You'd have to show more code to see what you can do about it.
I agree completely with @fire7side : Make sure it's not calling it all the time; you can post your code if you're unsure. One other thing that people don't always know when they are new, is that if you deactivate the game object, the coroutine will not continue to run.. so make sure you haven't done that.
this is the code. Flags are updated in different places and control the code flow Code (CSharp): void Start() { // some other code .... // .... // end other code GameUtility.levelStarted = true; } IEnumerator nextFigureTimer() { yield return new WaitForSeconds (5f); GameUtility.prepareNextFigure = true; } void Update () { if (GameUtility.levelStarted && !GameUtility.levelEnded) { // Stop game execution if (GameUtility.gameOver) { GameUtility.gameOver = false; GameUtility.levelEnded = true; LevelComplete(); return; } if (spawner.totalPlacedFigures == 0 && spawner.totalGeneratedFigures == 0) { spawner.CreateNextFigure(); GameUtility.ActivateNextFigure(); } if (GameUtility.figurePlaced) { GameUtility.figurePlaced = false; iterationCounter++; // Start Moving prepared figure GameUtility.playerFigure.GetComponent<FigureColliding>().enabled = true; GameUtility.playerFigure.GetComponent<MoveFigure>().enabled = true; GameUtility.playerFigure.GetComponent<SetSortingLayer>().sortingOrder = GameUtility.currentSortingOrder; GameUtility.currentSortingOrder++; StartCoroutine(nextFigureTimer()); } if (GameUtility.prepareNextFigure) { GameUtility.prepareNextFigure = false; if (spawner.CreateNextFigure()) { Debug.Log("All figures are used -> finish level"); LevelComplete(); } } } }
Pleas read this thread for when you post code on the forums: https://forum.unity.com/threads/using-code-tags-properly.143875/ Nothing there is really telling me why you wouldn't finish a coroutine.. If the game object isn't disabled (or destroyed) while it's running, and it has started, the coroutine should finish, afaik.
Thanks for pointing me to tagging code. AT one of the code parts I disbale a script for moving, script for colliding is active. WIll try to reorganize the code in order to achieve my goal. Thank you
Using a timer isn't ideal for that situation. I'm guessing you are waiting for <MoveFigure> to finish. If that is so, then MoveFigure should supply the ending point for the coroutine. Something like: Code (csharp): IEnumerator nextFigureTimer() { while(figureMoving == true){ yield return new WaitForSeconds (1f); Debug.Log("waiting for figureMove"); } Debug.Log("next figure =true"); prepareNextFigure = true; } Then you have MoveFigure change the bool when it is finished. There are other ways, but timers don't always work the way you think they should because programs can bog down, etc. If it's just one function you can easily wait for that to finish. Just check the section in the manual on coroutines. If it was me, I would move the figure using a coroutine and then just change prepareNextFigure to true when the coroutine was finished.
Hi all, thank you for supporting me. Since the game actions happening asynchronously I decide to make some code refactoring and now I am using events and delagates to drive code functionality. This solve most of my problems related to figure movement. I realize that coroutines can most likely be used in a places when there have to be some time gaps between actions.