A specific image to start with

I’m using this script unifycommunity.com and I want to start an animation on for example the third image, or stop at the 6th image. how should I do it?

You could make it into a function so it'll take parameters, it's a bit easier to handle when you have several objects needed to animate. Another good thing to do is to use different atlases and switch them on demand and also be able to just run the animation once.

Try out something like this:

var textureAtlases : Textxure2D[]; //The different atlases this object is using
private var colCount : int = 4; //How many columns the atlas has
private var rowCount : int = 1; //how many rows the atlas has
private var rowNumber : int = 0; //Starting row number
private var colNumber : int = 4; //Starting col number
private var fps : int = 20; //The frame update
@HideInInspector var animationIsDone : boolean = false; //The return if just played once

private var offset : Vector2;
private var startIndex = -1;

function SetSpriteAnimation (atlas : int, colCount : int, rowCount : int, rowNumber : int, colNumber : int, totalCells : int, fps : int, repeat : boolean) {
    if (renderer.material.mainTexture != textureAtlases[atlas]) {
         renderer.material.mainTexture = textureAtlases[atlas];
         animationIsDone = false;
    }

    //Calculate the index
    var index : int = Time.time * fps;

    //Repeat or return at exhaust of all cells
    if (repeat) {
        index = index % totalCells;
        animationIsDone = false;
    } else {
        if (startIndex == -1) startIndex = index;
        index -= startIndex;
        if (index > totalCells) {
            startIndex = -1;
            animationIsDone = true;
            return;
        }
    }

    var size : Vector2 = Vector2(1.0/colCount, 1.0/rowCount);
    var uIndex = index % colCount;
    var vIndex = index / colCount;

    offset = Vector2 ((uIndex+colNumber) * size.x, (1.0 - size.y) - (vIndex+rowNumber) * size.y);

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

}

Name it AnimationScript and attach it to a GameObject which should be animated, then from the code of that GameObject (its behavior, AI-script or what ever you might have) run it like this:

private var ani : AnimationScript;

function Start () {
    ani = GetComponent(AnimationScript);
}

function Update () {
    //Atlas, colCount, rowCount, rowNumber, colNumber, totalCells, fps, repeat
    ani.SetSpriteAnimation(0, 4, 4, 0, 0, 8, 20, true);
}

You can then run a case switch or what ever you like to switch between animations in a very old fashioned and easy manor.

To get the callback of the animation being done you can do this (there's probably an even better way but this works):

if (someBoolean) {
   ani.SetSpriteAnimation(0, 4, 4, 0, 0, 7, 20, false); //Send one frame less than usual to not get a flicker when switching to another animation
   if (ani.animationIsDone) someBoolean = false; //Check if the animation is done
}

To start and end on different locations you set `rowNumber`, `colNumber` and `totalCells` to different values when you call `SetSpriteAnimation()`.

This particular code is not tested, so feel free to come back with errors. However I have a similar solution that runs fine on lots of objects on a 3G iOS device.

(Name it AnimationScript and attach it to a GameObject which should be animated, then from the code of that GameObject (its behavior, AI-script or what ever you might have) run it like this)

i do not understand this step…when i play the game…it always said there is no render attach to the gameobject or Object reference not set to an instance of an object…