Loop code just stops

Hi all.

I’m trying to make my ground for my game move for 1 minute then get set to inactive but when I get to my second Corrutine it just stops

This is what I’ve so far done:

using UnityEngine;
using System.Collections;

public class GroundMovement : MonoBehaviour {

private int groundCountdown;
private int groundMovementCountDown;
private float movementAmount;

// Use this for initialization
void Start () {
    movementAmount = -0.1385f;
    groundCountdown = 60;
    groundMovementCountDown = 100;
     StartCoroutine(BeginningMovement());
}

public IEnumerator BeginningMovement()
{
    transform.Translate(new Vector3(movementAmount, 0.0f, 0.0f));
    yield return new WaitForSeconds(0.001f);
    groundMovementCountDown = groundMovementCountDown - 1;
    if (groundMovementCountDown == 0)
    {
        groundCountdown = groundCountdown - 1;
        transform.position = new Vector3(13.85f, -4.42f, 0.0f);
        groundMovementCountDown = 200;
        StartCoroutine(Movement());
    } else
    {
        StartCoroutine(BeginningMovement());
    }
}

public IEnumerator Movement () {
 
    transform.Translate(new Vector3(movementAmount, 0.0f, 0.0f));
    yield return new WaitForSeconds(0.001f);
    groundMovementCountDown = groundMovementCountDown - 1;
    if (groundMovementCountDown == 0)
    {
        groundCountdown = groundCountdown - 1;
        transform.position = new Vector3(13.85f, -4.42f, 0.0f);
    } else
    {
        groundMovementCountDown = 200;
        transform.position = new Vector3(13.85f, -4.42f, 0.0f);
        StartCoroutine(Movement());
    } if (groundCountdown == 0)
    {
        gameObject.SetActive(false);
    }
}

}

Any suggestions would be greatly appreciated.

Thanks in advance :slight_smile:

if (groundMovementCountDown == 0)
{
groundCountdown = groundCountdown - 1;
transform.position = new Vector3(13.85f, -4.42f, 0.0f);
} else
{
groundMovementCountDown = 200;
transform.position = new Vector3(13.85f, -4.42f, 0.0f);
StartCoroutine(Movement());
}

it seems your code checks if groundMovementCountDown =0, if this is false it sets it to 200, so the code in your if statement will never occur and the loop will continue forever.

remove the part where you set groundMovementCountDown back to 200 and it should work. However theres no need to keep starting your coroutine. Use a while loop and it will be much cleaner. for example something like this:

public IEnumerator BeginningMovement()
 {
	while (groundMovementCountDown != 0) {
		transform.Translate(new Vector3(movementAmount, 0.0f, 0.0f));
		groundMovementCountDown--;
		yield return new WaitForSeconds(1);		
	}    
 }

So if groundMovementCountDown = 60, then this coroutine will keep moving your GameObject by the distance “movementAmount” in the x direction, once a second for a minute. If I’m not mistaken this will achieve what you want but with considerably less code.

It isn’t a good idea to loop a coroutine by having it call itself again at the end. You should use a for loop within it instead.

Instead of using this:

 yield return new WaitForSeconds(0.001f);
 groundMovementCountDown = groundMovementCountDown - 1;

…and then having the coroutine call itself at the end unless it has reached 0 you should use something like this:

IEnumerator BeginningMovement()
{
    for (int i = groundCountdown; i >= 0; i--)
    {
        print(i);
        if (i == 0)
        {
            print("end loop");
            //Do something
        }
        yield return new WaitForSeconds(0.001f);
    }
}

Rather than waiting for 0.001 seconds 60 times though you are better off using a lower number for groundCountdown and a higher number of seconds to wait.