Hi there, i have a problem with an array and I don’t understand why I have this error. Here is a code
private var message = 0;
var maljbert1 : GameObject;
var maljbert2 : GameObject;
private var x : int;
var a : int;
var texture : Texture;
private var is = 0;
function Start () {
a = Mathf.RoundToInt(Random.Range (1,4)) ;
Debug.Log (a);
}
function Update () {
if (a>1){
maljbert1.renderer.material.mainTexture = texture[is];
is += 1;
}
}
And every time when I hit play button I recieve message : IndexOutOfRangeException: Array index is out of range.
I have GameObject with four textures and this script attatched to it . I hope you can help me.
Cheslav
You are looking up the texture using a variable called is which increments with no limit that I can see, that will quickly go out of range.
You specify a in start, if it is greater than one then you will just keep incrementing* is* forever - this will cause the index to be out of range.
I don’t think you have quite thought through that logic of what your “a” and “is” variables are doing - I’m guessing you really want to be setting a texture based off the value of “a” but can’t be sure
you made…private var is=0;
but you declared range as…Random.Range(1,4),
make it… is=Random.Range(0,4),
and assign five diff. textures to array… manually or automatically…
texture[0]=Texture0;
texture[1]=Texture1;
texture[2]=Texture2;
texture[3]=Texture3;
texture[4]=Texture4;