How to stop animated texture looping in Unity Indie?

Hi guys,

I am using this script to animated my explosion “sprite” but I can’t stop it from looping. How would I do this? I commented out the repeat part of the script but it did nothing…

 var uvAnimationTileX = 4; //Here you can place the number of columns of your sheet. 
                           //The above sheet has 4

var uvAnimationTileY = 1; //Here you can place the number of rows of your sheet. 
                          //The above sheet has 1
var framesPerSecond = 10.0;

function Update ()
{
    // Calculate index
    var index : int = 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);
}

Add the script when you need it. Destroy gameobject when you don’t need it anymore and respawn it without the script component. (Keep an eye on memory leaks: search for “destroy” and “memory leak”, “instanciate”, “garbage collector” ). But why don’t you want to use booleans and if-loops?

http://unity3d.com/support/documentation/ScriptReference/GameObject.AddComponent.html

This code doesn’t just loop, it also starts running from the beginning of the game. Here’s a rewrite:

 var uvAnimationTileX = 4; //Here you can place the number of columns of your sheet. 
                           //The above sheet has 4

var uvAnimationTileY = 1; //Here you can place the number of rows of your sheet. 
                          //The above sheet has 1
var framesPerSecond = 10.0;
private var t = 0.0;

function Update ()
{
    // Calculate index
    t += Time.deltaTime;
    var index : int = t * framesPerSecond;
	
	// stop when exhausting all frames
    if (index >= uvAnimationTileX * uvAnimationTileY)
        return;
    
    // 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);
}

Note that while I wrote return, probably what you actually want is to Destroy the gameObject, since the explosion will stay at the last frame unless you reset t to 0.