Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

not an iterator interface type

Discussion in 'Scripting' started by Luxio Chaosyn, Feb 20, 2015.

  1. Luxio Chaosyn

    Luxio Chaosyn

    Joined:
    Feb 20, 2015
    Posts:
    3
    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
     
  2. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    change void Update() to IEnumerator Update()

    yield statement returns an IEnumerator, not a void
     
  3. Luxio Chaosyn

    Luxio Chaosyn

    Joined:
    Feb 20, 2015
    Posts:
    3
    ok thanks ^^
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    That won't work because Update can't be a Coroutine.
     
    Kiwasi likes this.
  5. gtzpower

    gtzpower

    Joined:
    Jan 23, 2011
    Posts:
    318
    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.
    http://docs.unity3d.com/Manual/Coroutines.html

    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.
     
    Kiwasi likes this.
  6. Luxio Chaosyn

    Luxio Chaosyn

    Joined:
    Feb 20, 2015
    Posts:
    3
    ks

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