- Home /
Error cannot be an iterator block
So I ran into this error in my update function and I have no idea what it means, or what happened. It seemed to happen when I added my pause to my if statement. Here's the error...
error CS1624: The body of redical.Update()' cannot be an iterator block because
void' is not an iterator interface type
Here's my code
void Update () {
RaycastHit hit;
float distance;
float waitTime;
waitTime = 3.0f;
if (Physics.Raycast (new Ray (CameraFacing.transform.position, CameraFacing.transform.rotation * Vector3.forward), out hit)) {
distance = hit.distance;
if(hit.collider.gameObject.tag == "found"){
yield return new WaitForSeconds (waitTime);
if(hit.collider.gameObject.tag == "found"){
foundObject = hit.collider.gameObject;
Destroy(foundObject);
//x++;
//print(x);
}
}
} else {
distance = CameraFacing.farClipPlane * 0.95f;
}
transform.position = CameraFacing.transform.position + CameraFacing.transform.rotation * Vector3.forward * distance;
transform.LookAt (CameraFacing.transform.position);
transform.Rotate (0.0f, 180.0f, 0.0f);
}
}
You can't have a "yield return" in there because Update doesn't (and can't) return an IEnumerator.
Generally speaking you should only be putting "yields" in Coroutines (which must return IEnumerators).
You probably want to take this code out of Update and put it in a coroutine (which you could start from the update function).
You don't want to be waiting in an update routine anyhow (blocking your updates will hurt your FPS).
If you need to do something else in response (that is going to delay or take some time) you should be spinning off a coroutine so it doesn't block the main thread.
Answer by pako · Jun 09, 2015 at 02:42 PM
The "yield return new WaitForSeconds (waitTime)" statement (line 11) is what's causing the error. It should only be used inside iterator blocks (IEnumerator):
Your answer
