- Home /
Question by
Mateusz Gryczan · Nov 16, 2014 at 10:06 PM ·
2djavascriptspritesspritesheet
random sprite javascript
I try to generate random sprite, but i have "Array index is out of range" I don't understand what i am doing wrong. Here is my code:
#pragma strict
var sprites : Sprite[];
var resourceName : String = "Sprites/spriteLetters";
var currentSprite : int = -1;
function Start () {
if(resourceName != "") {
sprites = Resources.LoadAll.<Sprite>(resourceName);
if(currentSprite == -1) {
currentSprite = Random.Range(0, sprites.Length);
}
else if(currentSprite > sprites.Length) {
currentSprite = sprites.Length - 1;
}
GetComponent(SpriteRenderer).sprite = sprites[currentSprite];
}
}
Comment