I'm beginner in Unity and i have an problem.
In Update function , i call StartCoroutine(move()); That is my move function:
public IEnumerator move()
{
Vector2 tmp = new Vector2(14.47f, transform.position.y);
bool hasReachedTarget = false;
while (!hasReachedTarget)
{
if (transform.position.x < tmp.x)
{
transform.position =
Vector3.MoveTowards(transform.position, tmp, moveSpeed * 0.01f * Time.deltaTime);
yield return null;
}
else
{
hasReachedTarget = true;
}
}
Debug.Log("ct "+count);
count++;
if (!isHandled)
{
isHandled = true;
transform.position = tmp;
GameStoryManager.curGameStoryIndex++;
animator.SetBool(IS_MOVE_NAME, false);
}
}
My problem is when when this function run, it call to it call to it a lot of times; By use Debug.Log("ct "+count) , i see a lot of count.
I don't know why did it happen. Please someone explain it to me. Thanks for yours answer.
I realized that the reason is i call move function directly in update funtion, thus it start new instance of coroutine every update function is called. I add some condition to call move function and it worked.