2d animation from texture

hi guys,im having trouble with animating a texture and having the ability to restart the animation,i just cant restart it. here is the code that i have for animating the texture:

var framesPerSecond = 10.0;
var uIndex;
var index : int;
var offset : Vector2;
var size : Vector2;

function Update () {
    index = Time.time * framesPerSecond;
    // repeat when exhausting all frames
    index = index % (uvAnimationTileX * uvAnimationTileY);
    // Size of every tile
    var size = Vector2 (1.0 / uvAnimationTileX, 1.0 / uvAnimationTileY);

    // split into horizontal and vertical index
    var uIndex = index % uvAnimationTileX;
    var vIndex = index / uvAnimationTileX;

    // build offset
    // v coordinate is the bottom of the image in opengl so we need to invert.
    var offset = Vector2 (uIndex * size.x, 1.0 - size.y - vIndex * size.y);

    renderer.material.SetTextureOffset ("_MainTex", offset);
    renderer.material.SetTextureScale ("_MainTex", size);
}

any ideas how i can reset the animation,so it will start from the beginning(i need to reset the animation wen i go to a new animation=a new texture) thanks in advance!8-)

Hello,
old question, but nobody answered, so i’ll give a try.
First all, I don’t use the code of the script like u did(wiki), i made my own after playing around in the inspector values and reduced it to the most important things.

Sorry I use C# but in JS you can just put it into the start function or a normal function and call it once i think, because the for-loops never end.

You have to set variables in the inspector, be sure that AnimationCyclePerSecond isn’t 0

using UnityEngine;
using System.Collections;

public class TextureAnimationScript : MonoBehaviour
{
	public int TilesX;
	public int TilesY;
	public float AnimationCyclesPerSecond;

	void Start ()
	{
		renderer.material.mainTextureScale = new Vector2(1.0f / TilesX, 1.0f / TilesY);
		StartCoroutine(ChangeOffSet());
	}
	
	IEnumerator ChangeOffSet ()
	{
		for(int i = TilesY - 1; i > -1; i--)
		{
			for(int j = 0; j < TilesX; j++)
			{
				renderer.material.mainTextureOffset = new Vector2(1.0f / TilesX * j, 1.0f / TilesY * i);
				yield return new WaitForSeconds(1.0f / (TilesX * TilesY * AnimationCyclesPerSecond));
				if(i == 0 && j == TilesX - 1)
				{
					i = TilesY;
				}
			}
		}
	}
}

You need to store the time when the animation was started and then calculate

index = Time.time - AnimationStartTime * framesPerSecond;

Resetting the Animation is then as easy as setting AnimationStartTime to the current time.