- Home /
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 :-)
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 ());
}
}
Answer by Addyarb · Mar 30, 2015 at 11:20 PM
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.
Do you know what i am doing to make it repeat too fast?
Well it's sort of hard for me to explain.
$$anonymous$$y guess to what is happening internally is that since you're using a non-conditional initializer (your Input.Get$$anonymous$$ey) that you are calling the coroutine over and over. This sets of a series of instances of that coroutine that will appear to make things seem to happen faster since you actually have many coroutines happening ins$$anonymous$$d of just one.
The solution (if this is the problem) would be to set a bool true whenever the coroutine is running, and set it false as soon as it's done running. That way you know only one coroutine is running.
Another way to check to see what's going on it to put something like this in your coroutine:
yield return new WaitForSeconds(1);
Debug.log("Waited 1 second");
yield return new WaitForSeconds(1);
Debug.log("Waited 2 seconds");
yield return new WaitForSeconds(1);
Debug.log("Waited 3 seconds");
yield return new WaitForSeconds(1);
Debug.log("Waited 4 seconds");
yield return new WaitForSeconds(1);
Debug.log("Waited 5 seconds");
Obviously if you see multiple, identical, or out-of-order messages, then you are running many coroutines.
no because i put the condition is moving = false in there. and after its hit, is moving = true.
You are right. The only way I can get it to work is by changing StopCoroutine($$anonymous$$ove()); to StopAllCoroutines();
Hmm why is that do you think? im not being sarcastic im curious too :P
Answer by ian_sorbello · Mar 31, 2015 at 01:25 AM
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(;;) 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");
}
}
}
Answer by eelstork · Mar 31, 2015 at 02:25 AM
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:
Wait for input
Move the object
Wait for cooldown.
There are other ways to do this but in your case this may be easier and simpler.
Your answer
![](https://koobas.hobune.stream/wayback/20220613191724im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
spawn timer problem 2 Answers
making a 2D character Speak for different lenghts of time 2 Answers
Why do you use Coroutines and how do you use them? 1 Answer
[ Coroutine: Move Next ] CPU usage 0 Answers
Cancel IEnumerator in progress 3 Answers