Animataion of a Sprite v2

Hey Unity Community, I am trying to animate this sprite, in this scene. I have asked this question before, and some cool people helped me to get started.

For some reason nothing is happening when I play the game.

This is the sprite sheet:
13183-asuka.png

To make the sprite move, it has to increase the OffsetX from (0,0.33,0.66) and decrease the offsetY from (0.66,0.33,0). When the coordinates get to X = 0.66, and the Y is at zero, there is nothing there, so I just reset the coordinates back at the first sprite.

Here is the code:

using UnityEngine;
using System.Collections;

public class SpriteSheet_Asuka : MonoBehaviour
{
    // Use this for initialization
    //OffsetX and offsetY to set where it the sprite starts.
    public float offsetX = 0f;
    public float offsetY = 0.66f;
    public float tilingX = 0.3f;
    public float tilintY = 0.3f;


    void Start()
    {

        animate();


    }

    // Update is called once per frame
    void Update()
    {

        renderer.material.mainTextureOffset = new Vector2(offsetX, offsetY);
        renderer.material.mainTextureScale = new Vector2(tilingX, tilintY);

    }

    IEnumerable animate()
    {
       int currentFrames = 0;
        int totalFrames = 7;
        while (totalFrames > currentFrames)
        {
            renderer.material.mainTextureOffset = new Vector2(offsetX, offsetY);

            if (offsetX > 0.66f)
            {
                offsetX = 0f;
                offsetY -= 0.33f;
            }
            else if (offsetY == 0f && offsetX == 0.66f)
            {
                offsetX = 0f;
                offsetY = 0.66f;
            }
            else
            {
                offsetX += 0.33f;
                currentFrames++;
            }


            if (currentFrames == 7)
            {
                currentFrames = 0;
            }

        }
        yield return new WaitForSeconds(1);

    }


}

You need to use StartCoroutine() in C#. Line 17 should be:

StartCoroutine(animate());

Coroutines need to be of type ‘IEnumerator’ not ‘IEnumerable’, so line 31 should be:

IEnumerator animate()

Your ‘yield’ needs to be inside your while() loop. So move it from line 62 to line 60. If you don’t do this, you will hang Unity.

I think there are some issues with your step logic for each frame, but these three changes will get you up and running.