Coroutine WaitForSeconds is varying? How to fix?

I know this code is long, so the IEnumerator is at the bottom, and the initiation is at the top of update. My code is in an attempt to, while holding a move button, move in that direction every half second. Its speeding up, meaning it’s varying.

EDIT

I cut the code down so its only important stuff :slight_smile:

EDIT 2

Also, when i cut the code, i cut things in between and even though i separated parts, it kept them as one piece of code.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Cube : MonoBehaviour {
	
	public GameObject [] ManagerGet = new GameObject [2];
	public GameObject AddButtonGet;
	public Text TextGet;
	public int DataValue;
	public bool CanSubtract;
	public bool IsSelectable;
	public bool IsMoving;

	public enum Types {
		Cube,
		Tree
	};
	public Types Type;

	void Update () {
		if (Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.D))
		{
			if (IsMoving == false)
			{
				StartCoroutine (Move ());
				IsMoving = true;
			}
		}
		else
		{
			StopCoroutine (Move ());
			IsMoving = false;
		}

	IEnumerator Move () {
		if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
		{
			if (Input.GetKey (KeyCode.W))
			{
				if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
				{
					transform.position += Vector3.up;
				}
				if (ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube].transform.position.y >= Camera.main.transform.position.y + 4)
				{
					Camera.main.transform.position += Vector3.up;
				}
				if (Input.GetKey (KeyCode.LeftShift) && ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue && ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube] != null)
				{
					Instantiate (gameObject, transform.position, Quaternion.identity);
				}
			}
			if (Input.GetKey (KeyCode.A))
			{
				if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
				{
					transform.position += Vector3.left;
				}
				if (ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube].transform.position.x <= Camera.main.transform.position.x - 5)
				{
					Camera.main.transform.position += Vector3.left;
				}
				if (Input.GetKey (KeyCode.LeftShift) && ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue && ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube] != null)
				{
					Instantiate (gameObject, transform.position, Quaternion.identity);
				}
			}
			if (Input.GetKey (KeyCode.S))
			{
				if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
				{
					transform.position += Vector3.down;
				}
				if (ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube].transform.position.y <= Camera.main.transform.position.y - 4)
				{
					Camera.main.transform.position += Vector3.down;
				}
				if (Input.GetKey (KeyCode.LeftShift) && ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue && ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube] != null)
				{
					Instantiate (gameObject, transform.position, Quaternion.identity);
				}
			}
			if (Input.GetKey (KeyCode.D))
			{
				if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
				{
					transform.position += Vector3.right;
				}
				if (ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube].transform.position.x >= Camera.main.transform.position.x + 5)
				{
					Camera.main.transform.position += Vector3.right;
				}
				if (Input.GetKey (KeyCode.LeftShift) && ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue && ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube] != null)
				{
					Instantiate (gameObject, transform.position, Quaternion.identity);
				}
			}
		}
		yield return new WaitForSeconds (0.5f);
		Debug.Log ("YES");
		StartCoroutine (Move ());
	}
}

I’ve had similar issues. While I’m not an expert with IEnumerators, something like this usually helps:
This is usually known as condition validation.

 IEnumerator MyCoRoutine(){
    if(!yielding){
    yield return new WaitForSeconds(secondsToWait);
    yielding = false;
    }
    }

Edit: Basically what I’m getting at is that you are probably starting the coroutine multiple times, and that is giving you unpredictable behavior. It is better to only run the coroutine once and wait for the bool to “expire” before beginning a new one.

Calling StartCoroutine( ) within the coroutine itself isn’t good. Also, calling StartCoroutine(Move()) in the Update loop is the reason why you’ve got re-entrency issues. You’re starting a Move() function call every frame, so it’s definitely the reason for your problem.

Change the CoRoutine to this:

IEnumerator Move () {
  for(;;) {
     // Do all of your input checking here
     // ...
     // Then wait
     yield return new WaitForSeconds(0.5f);
  }
}

Note: You only start the coroutine once, and the for(;:wink: keeps the coroutine going forever, you don’t need to start it again - there’s only one loop in play- and your timings will be accurate.

Therefore, don’t call StartCoroutine from the Update() loop - start it up only once on Awake() or Start().

You might need to change the logic about checking if you’re moving etc. within the Coroutine itself.

EDIT: I wrote the code for how this would work. I couldn’t check if it compiled, as I don’t have your classes to compile against - but here’s how it should work:

using UnityEngine;
using System.Collections;


public class Cube : MonoBehaviour {
		
	public GameObject [] ManagerGet = new GameObject [2];
	public GameObject AddButtonGet;
	public Text TextGet;
	public int DataValue;
	public bool CanSubtract;
	public bool IsSelectable;
	public bool IsMoving;
	
	public enum Types {
		Cube,
		Tree
	};
	public Types Type;

	void Start() {
		// Just start the coroutine once
		StartCoroutine (Move());
	}


	void Update () {
		// Do something else here - the coroutine is looking after itself
	}

	IEnumerator Move () {
		for(;;) {
			if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
			{
				if (Input.GetKey (KeyCode.W))
				{
					IsMoving = true;

					if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
					{
						transform.position += Vector3.up;
					}
					if (ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube].transform.position.y >= Camera.main.transform.position.y + 4)
					{
						Camera.main.transform.position += Vector3.up;
					}
					if (Input.GetKey (KeyCode.LeftShift) && ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue && ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube] != null)
					{
						Instantiate (gameObject, transform.position, Quaternion.identity);
					}
				}
				if (Input.GetKey (KeyCode.A))
				{
					IsMoving = true;

					if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
					{
						transform.position += Vector3.left;
					}
					if (ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube].transform.position.x <= Camera.main.transform.position.x - 5)
					{
						Camera.main.transform.position += Vector3.left;
					}
					if (Input.GetKey (KeyCode.LeftShift) && ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue && ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube] != null)
					{
						Instantiate (gameObject, transform.position, Quaternion.identity);
					}
				}
				if (Input.GetKey (KeyCode.S))
				{
					IsMoving = true;

					if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
					{
						transform.position += Vector3.down;
					}
					if (ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube].transform.position.y <= Camera.main.transform.position.y - 4)
					{
						Camera.main.transform.position += Vector3.down;
					}
					if (Input.GetKey (KeyCode.LeftShift) && ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue && ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube] != null)
					{
						Instantiate (gameObject, transform.position, Quaternion.identity);
					}
				}
				if (Input.GetKey (KeyCode.D))
				{
					IsMoving = true;

					if (ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue)
					{
						transform.position += Vector3.right;
					}
					if (ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube].transform.position.x >= Camera.main.transform.position.x + 5)
					{
						Camera.main.transform.position += Vector3.right;
					}
					if (Input.GetKey (KeyCode.LeftShift) && ManagerGet [0].GetComponent <CubeManager> ().CurrentCube == DataValue && ManagerGet [0].GetComponent <CubeManager> ().Cubes [ManagerGet [0].GetComponent <CubeManager> ().CurrentCube] != null)
					{
						Instantiate (gameObject, transform.position, Quaternion.identity);
					}
				}
			}

			// Wait before we loop again
			yield return new WaitForSeconds (0.5f);
			Debug.Log ("YES");
		}
	}
}

I’m with Ian here, in that sorry I’m not bothered figuring exactly what you’re doing with your cubes (instantiating cubes along a path determined by keyboard input?) but here is a script that lets you move a cube using the keyboard, factoring in your delay constraint.

using UnityEngine;
using System.Collections;

public class SteppedMotionController : MonoBehaviour {

void OnEnable() { StartCoroutine ("Move"); }

private IEnumerator Move(){

	while (enabled) yield return StartCoroutine("Step");

}

private IEnumerator Step(){

	Vector3 u = Vector3.zero;

	// Wait for input

	while (u==Vector3.zero) {

		if (Input.GetKey (KeyCode.W)) u.y = 1.0f;
		if (Input.GetKey (KeyCode.A)) u.x = -1.0f;
		if (Input.GetKey (KeyCode.S)) u.y = -1.0f;
		if (Input.GetKey (KeyCode.D)) u.x = 1.0f;

		yield return null;

	}

	// Move

	transform.position += u;

	// Wait for cooldown

	yield return new WaitForSeconds (1.0f);

}

}

Although simplified, the advantage is that since I removed your dependencies to external stuff I could test this to make sure it actually works.
As you can see the idea is the same as what Ian suggested, where you run a “Step” function which does three things in a sequence:

  1. Wait for input
  2. Move the object
  3. Wait for cooldown.

There are other ways to do this but in your case this may be easier and simpler.