not an iterator interface type

Sorry to bother you but i’ve a problem with my code, i don’t know how to use properly the command waitforsecond. my code look like that:
using UnityEngine;
using System.Collections;

public class animation : MonoBehaviour {

public GameObject Soldier_0;
public Animator Player;
private int dep1;
private int dep2;
private int up1;
private int up2;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update ()
{
dep1 = transform.position.x;
up1 = transform.position.y;
yield return new WaitForSeconds(0.04);
dep2 = transform.position.x;
up2 = transform.position.y;
if (dep1 > dep2)
{
Player.SetBool(“Run”, true);
Soldier_0.transform.localScale = new Vector3(-1, 1, 1);
Soldier_0.transform.rotation = new Quaternion(0, 0, 0, 0);
}
if (dep1 < dep2)
{
Player.SetBool(“Run”, true);
Soldier_0.transform.localScale = new Vector3(1, 1, 1);
Soldier_0.transform.rotation = new Quaternion(0, 0, 0, 0);
}
if ((dep1 = dep2) && (up1 = up2))
{
Player.SetBool(“Run”, false);
}
}
}

and the error code is :
Assets/script/animation.cs(19,14): error CS1624: The body of animation.Update()' cannot be an iterator block because void’ is not an iterator interface type

thanks in advence for your reply

change void Update() to IEnumerator Update()

yield statement returns an IEnumerator, not a void

ok thanks ^^

That won’t work because Update can’t be a Coroutine.

1 Like

You need to create another function of type IEnumerable and call StartCoroutine on it if you want to use yield like this.

Here are some examples to get you going.

On a side note, it looks like you are trying to play a run animation if the object is moving. Does the object have a rigid body attached? If so you could just check rigidbody.velocity.x and rigidbody.velocity.y. This would be much easier.

1 Like

ks

ohw ^^ thx man that’s exactly waht i’ve been looking for ^^
many than