Fix "Array index is out of range" problem

so I made a small bit of code to play a 3 sec video, after it plays quite well this error pops up and pauses the game: “IndexOutOfRangeException: Array index is out of range”.

How is this to be fixed? I tried to add some if() to the code, but some are just useless.

public Texture2D[] images;
private int i=0;
public float currentImageTime = 0f;

void Update ()
{
	currentImageTime += Time.deltaTime;
	ChangeImage();
}

void OnGUI() 
{
	i++;
	GUI.DrawTexture(new Rect(100,100,500,500), images*);	*
  • }*

  • void ChangeImage()*

  • {*

  • if(i<=197)*

  •   {*
    
  •   if(currentImageTime >= 300f)*
    
  •   { 	*
    
  •   	OnGUI ();*
    
  •   	currentImageTime = 0f;*
    
  •   }*
    
  •   else{}*
    
  •   }*
    
  • }*

Change:

void OnGUI() 
{
    i++;
    GUI.DrawTexture(new Rect(100,100,500,500), images*);* 

}
to:
void OnGUI(){
for(i = 0; i < images.length; i++){
GUI.DrawTexture(new Rect(100,100,500,500), images*);*
}
}
that way you wont have the DrawTexture trying to display images that do not exist. Also, open your console by going into Window > Console (or typing ctrl + shift + c) and disable the pause on error button to stop your game from pausing on non-threatening errors.