Using Coroutines and Update together
I am currently working on my first game, which is very similar to Tetris Attack. I'm working on the grid, and am running into a weird bug.
First, when the user clicks on two tiles, if they are adjacent, they will swap places. I got this working by using a coroutine. I then tried to get the tiles to move up, which I did in the Update function, by moving the transforms up by a small amount. These two things work independently.
The problem happens when I try to swap two pieces while the tiles are moving up, the two tiles stop moving up, switch places, then try to move up. This causes problem, as the rest of the tiles in the grid continue to move up while they are swapping places, and I'm not really sure what is going on.
TL;DR I have one game object that whose transform is being altered in both a coroutine and in the Update function, which is causing problems.
I think the solution is to use solely coroutines or the Update function, but not both. I'm just trying to get a better understanding of what is going on. If I need to add code or pictures, let me know and I'll add it.
Sounds like you are not stopping the old Coroutines properly? Hard to tell without seeing the code that is involved with the update and Coroutine. A Coroutine that yields by yield return null
gets called right after the Update of whatever instance that started the Coroutine so normally it should not be a problem to use both if you start the Coroutine on the same instance as the Update.
I'll add some code later to try and make this clearer. But my Coroutine isn't being called inside my Update function. The coroutine is being started on a script attached to an individual tile, because I planned on having some tile types that can't be swapped. The Update function that is moving the tiles up is in a script on the grid. I might also be organizing my project poorly
The StartCoroutine doesn't need to be called inside the Update method but it is a good practice to use StartCoroutine within the same Class because then only one class handles the Coroutine.
Your answer
![](https://koobas.hobune.stream/wayback/20220612110123im_/https://answers.unity.com/themes/thub/images/avi.jpg)