IndexOutOfRangeException: Array index is out of range problem

Hello,

I get this error where the console is saying:

IndexOutOfRangeException: Array index is out of range.

using UnityEngine;
using System.Collections;

public class plusone : MonoBehaviour {

	public Vector3 speed;
	public Sprite[] cicle;
	public Sprite cursprite;
	public GameObject spawn;
	public SpriteRenderer render;
	public int number;

	void Start () {
		render = spawn.GetComponent<SpriteRenderer> ();
	}

	public void Animate () {
		number = Random.Range (1, 4);
		cursprite = cicle[number];
		Instantiate (spawn, new Vector3 (0, 0, 0), Quaternion.identity);
		render = spawn.GetComponent<SpriteRenderer> ();
		render.sprite = cursprite;
	}
}

i am trying the whole day to get it right but its not working can anyone help me big thanks the full error is:

IndexOutOfRangeException: Array index is out of range.
plusone.Animate () (at Assets/scripts/effects/plusone.cs:19)
arrow.Update () (at Assets/scripts/arrow.cs:49)

Rick Grendel

How many elements are in the circle array? A safer bet would be to clamp the range to the length of the array.

public void Animate()
{
    if(circle.length<=0)
        return;

    number = Random.range(0,circle.length);
    cursprite = circle[number];
    Instantiate(spawn, Vector.Zero, Quaterion.identity);
    render.sprite = cursprite;
}

I assume the console is telling you what line the error is happening on. It’s usually very helpful to provide that information so we don’t have to look through the code and guess. But since we’re guessing, I’ll go through my thought process which will hopefully help you do your own looking when the next error comes along.

First, some info: An array is created with a certain number of elements in it. If you try to access an element using an index that is < 0 or >= the array length then you’ll get the index out of range error. The < 0 is hopefully obvious, the >= array length is because the first index in the array is at 0. So if you have an array of length 5, the valid indexes are 0, 1, 2, 3, 4. i.e. there are 5 elements, and element 5 is at index 4.

So, here’s what I did to look for your problem:

  • We’re dealing with arrays, so find where you’re accessing an array. This lead me to line 19 which appears to be the only place you’re accessing an array.
  • The array is “cicle”, so find where you declare that. It’s an array of sprites - Sprite[] cicle.
  • Arrays must be allocated before they’re used, so I scanned through your code again to find out where you allocate it. You don’t, so that is a potential problem. But the array is public and your script inherits from Monobehaviour, so it’s possible you’re creating the array in the inspector. I’ll proceed with that assumption.
  • Next I look at the index. You’re using the variable number which is set above to a random value between 1 and 4. Now, Random.Range has this fun little thing where if you pass floats as the range you’ll get a number between Start and End inclusive. Meaning you may get a value that matches End. But if you pass integers you get a number between Start and End exclusive, which means you will never get the end value. So in your case, since you’re passing integers, you’ll get a random value between 1 and 3.
  • And… that’s as far as I can go.

So my conclusion is either:

  • You’re not allocating the array at all (but that should result in a different error), or
  • The array gets created by the inspector, but it has fewer than 4 items in it, so the random index is out of range.