Image array/for comics

So I want to add a few images (comics) to my level just before it starts. Got something but not being a left brain person doesn’t help much. Here’s the script that gives me an array index out of range (also, it does the “we’re done here” before anything else instead of right after the last slide")

var imageArray : Texture[];
var currentImage : int;
var imageRect : Rect;
var buttonRect : Rect;
var customSkin : GUISkin;
var btnHeight = 10;
var btnWidth = 100;

function Start()
	{
	if(currentImage > 1)
	currentImage = 0;
    imageRect = Rect(0, 0, Screen.width, Screen.height);
	buttonRect = Rect(0, 0, Screen.width/4-btnWidth, Screen.height/10-btnHeight) ;
	if(currentImage < 2)
	Debug.Log("We're done here");
}

 

function OnGUI()

{
	GUI.skin = customSkin;
    GUI.Label(imageRect, imageArray[currentImage]);
    if(GUI.Button(buttonRect, "Next"))
	currentImage++;
}

Any help is greatly appreciated!

When you do currentImage++ in OnGUI, you don’t check that it’s going out of range. It will only work as long as currentImage < imageArray.Length, and when it exceeds that, you get the error. As for the Start function, everything in there executes during the first frame. It checks if currentImage > 1 and if so, sets it to 0 (which you don’t need to do, you can just set it to 0 without checking anything). Then it checks if currentImage < 2, which it is since you set it to 0, so it prints “We’re done here”. Then it runs the OnGUI function every frame after that.